I have two csv files like this:


user size
aaa   10
bbb   20
ccc   30


name value
www   5
mmm   8
ppp   9

I want to convert this to two separate HTML tables using a shell script. How do I do this?


Assume the fields in your files separated by spaces and you've only two fields in every row:

sed -E '1 s@^([^ ]*) *(.*)@<table><tr><th>\1</th><th>\2</th></tr>@ ; \
  2,$ s@^^([^ ]*) *(.*)@<tr><td>\1</td><td>\2</td></tr>@ ; \
  $ s,.*,&</table>,' test1.csv

The first line assumes the first line of test1.csv is table head. Open the <table> tag, a <tr> and enclose every field between <th>. After it closes the </tr>. The second line is similar but only transforms from the second line of test1.csv.

The third line appends the last line ($) the </table> to close the table tag (opened in first line).

You can do it with pure awk of course.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.