Home > OS >  Split filenames at delimiter and write to HTML table
Split filenames at delimiter and write to HTML table

Time:10-18

I am attempting to split some filenames which contain data in their names and export this into different columns in a HTML table. An example filename is below:

 10.129.18.225,9998,builtin-v10.conf

There are multiple files in the directory with the same format (IP address, Port Number, builtin-v(5,7,9 or 10) that I need to perform this action on as well. New files are constantly added and removed.

My aim is to be able to split the filename using the ',' as a delimiter/separator and import the different variables of the filename into a HTML table like below:

Collector IP Address Collector Port Netflow Version
10.129.18.225 9998 builtin-v10
10.0.0.0 9000 builtin-v9

I have taken a look at a few different posts which seem to be similar but I was just wondering the best way to achieve this in bash?

I have the following script at the moment but I don't think it is correct.

    #!/bin/bash

$file="/usr/local/flowsim/data/*.conf"
data=$(echo $file | cut -d"," -f1 | tr -d ",")

Collector=$(echo $file | cut -d"," -f1) >> "/usr/local/flowsim/active-flows.html"
Port=$(echo $file | cut -d"," -f2 | cut -d"," -f1)

Any suggestions or examples would be greatly appreciated!

CodePudding user response:

You can split the filename on commas by assigning bash variable IFS to it. Would you please try:

#!/bin/bash

for file in /usr/local/flowsim/data/*.conf; do                  # loop over the *.conf files
    if [[ -f $file ]]; then                                     # make sure $file exist
        f="$(basename "$file" ".conf")"                         # strip directory and suffix from $file
        IFS=, read -r data Collector Port <<< "$f"              # split "$f" on commas and assign variables
        # echo "$data $Collector $Port"                         # just to check the variables
        # add to the html table using the variables             # do your job here
    fi
done

CodePudding user response:

Another approach is to use the =~ operator in bash.

#!/usr/bin/env bash

for file in /usr/local/flowsim/data/*,*,*.conf; do
  [[ $file =~ (. ),(. ),(. ).* ]] &&
  ip=${BASH_REMATCH[1]}
  port=${BASH_REMATCH[2]}
  version=${BASH_REMATCH[3]}
  printf '%s\n%s\n%s\n' "$ip" "$port" "$version"
done

import the different variables of the filename into a HTML table

Something like this maybe.

#!/usr/bin/env bash

head='<!DOCTYPE html>
<html>
<style>
table, th, td {
  border:1px solid black;
}
</style>
<body>
<h2>A basic HTML table</h2>
<table style="width:50%">
  <tr>
    <th>Collector IP Addess</th>
    <th>Collector Port</th>
    <th>Netflow Version</th>
  </tr>'

tail='</table>
</body>
</html>'

printf '%s\n' "$head"

for file in /usr/local/flowsim/data/*,*,*.conf; do
  [[ $file =~ (. ),(. ),(. ).* ]] &&
  ip=${BASH_REMATCH[1]}
  port=${BASH_REMATCH[2]}
  version=${BASH_REMATCH[3]}
  printf ' <tr>\n    <td>%s</td>\n    <td>%s</td>\n    <td>%s</td>\n </tr>\n' "$ip" "$port" "$version"
done

printf '%s\n' "$tail"

Now you can point the script to a file, e.g.

my_script > output.html
  •  Tags:  
  • bash
  • Related