Convert CSV into a table structure within my bash scripts

enthuguy
enthuguy used Ask the Experts™
on
Hi bash experts,
would like to convert below log file separated by | into a nice table within my bash script. Is it possible? please help

lifray-portal-dev|464646464efef9||0|sometext
lifray-admin-dev|464646464efef9|ami-987654321|1|sometext
lifray-portal-test|464646464efef9||0|sometext
lifray-admin-test|464646464efef9||0|sometext
lifray-portal-sit1|464646464efef9||0|sometext
lifray-admin-sit1|464646464efef9|ami-987654321|2|sometext
lifray-portal-ppte|464646464efef9||0|sometext
lifray-admin-ppte|464646464efef9||0|sometext


found few links, but looks bit complex
https://www.unix.com/shell-programming-and-scripting/109997-csv-table-one-liner.html?_sm_au_=iqVvmlnQVN3VDHZrBJ3vvK7RJCBJt

https://unix.stackexchange.com/questions/105501/convert-csv-to-html-table?_sm_au_=iqVvmlnQVN3VDHZrBJ3vvK7RJCBJt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
If you have an idea of the maximum width of every column before processing the whole input file, you will avoid the complexity of the full awk solution found in https://www.unix.com/shell-programming-and-scripting/109997-csv-table-one-liner.html?_sm_au_=iqVvmlnQVN3VDHZrBJ3vvK7RJCBJt

Please tell us if you can estimate the maximal width of each column.
it is most likely much easier and efficient to do whatever you need on the fly rather than store the csv in bash tables. what is your end game, here ?

that said, the conversion could be quite trivial but you would need to indicate the format knowing that bash does not support multidimensional arrays.
assuming you want to convert to html


IFS='|' while read f1 f2 f3 f4 f5
do
  echo "<tr>";
  for f in "$f1" "$f2" "$f3" "$f4" "$f5"
  do echo "<td>$f</td>"
  done
  echo "</tr>"
done

or simpler with sed

sed -e '
  s/^/<tr><td>/
  s:$:</td></tr>:
  s:|:</td><td>:g
  1 s:^:<table>:
  $ s:$:</table>
'

Commented:
Assuming:
  1. you do not want a HTML table but a plain text,
  2. you know the maximal length of each field (I supposed fields of resp. 20, 15, 15 and 1 char for the first 4 fields and an unlimited number of chars for the last field)
  3. and input.tab contains your input file (which is not really a CSV file, since you use '|' instead of ','),
you can try this one liner:

awk -F '|' -e '{printf "%-20s | %-15s | %-15s | %1s | %s\n", $1,$2,$3,$4,$5}' input.tab

Open in new window

Author

Commented:
Thanks a lot Pierre and skull
I can tell max width. First 4 column 25, last column 50 (description column).

above solutions seems very interesting...will try those in update

Author

Commented:
Hi skull, sorry
how do I input the file for those two scripts please

HI Pierre, script produced a nice table output, is it possible to have row separator as well please?

Basically trying to output this on a jenkins console .
the input is expected on stdin.

Myscript << inputfile

Which will produce html output

If you just want a nice text output, use

columns -f '|'
Commented:
Adding a row separator is as easy as coding:
awk -F '|' -e 'BEGIN{
  rowseparator="+---------------------------+---------------------------+---------------------------+---------------------------+----------------------------------------------------+\n"; 
  print rowseparator;
}
{printf "| %-25s | %-25s | %-25s | %-25s | %-50s |\n%s", $1,$2,$3,$4,$5,rowseparator}' input.tab

Open in new window

Author

Commented:
thanks again, that really helped me to resume my task. A good start :)

Author

Commented:
thanks Pierre and skullnobrains

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial