Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

outputting a CSV file from shell

Hi I have an input file in this format.

[Header A]
key1 value1
key2 value2

[Header B]
header1    header2    header3   // separated by tab
1            2          3     //separated by tab
a            b          c     //separated by tab

[Header C]
a
b
c

I have to output whatever is in HeaderB as another csv file So the CSV file will look like

header1,header2,header3
1,2,3
a,b,c

I wrote a python script to do that but the requirement it to do it via a shell command. I don't have much idea about writing complicated shell commands. Can someone help me.

Thanks

like image 547
Ank Avatar asked Mar 31 '26 17:03

Ank


1 Answers

This will work for you:

awk -F'\t' '/\[Header B\]/{f=1;next} /^$/{f=0} f{gsub(/\t/,",");print}' file

Example

$ awk -F'\t' '/\[Header B\]/{f=1;next} /^$/{f=0} f{gsub(/\t/,",");print}' file
header1,header2,header3
1,2,3
a,b,c
like image 193
SiegeX Avatar answered Apr 03 '26 09:04

SiegeX