Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a header to awk output?

Tags:

linux

awk

I have a csv file that looks like below

"10.8.70.67","wireless",,"UTY_07_ISD",,26579
"10.8.70.69","wireless",,"RGB_34_FTR",,19780

I want to retrieve first, second and fourth column values (without quotes) and populate into a another csv in the below format.

IP          DEVICETYPE  DEVICENAME
10.8.70.67  wireless    UTY_07_ISD
10.8.70.69  wireless    RGB_34_FTR

I have used the below awk command

awk -F ',|,,' '{gsub(/"/,"",$1); gsub(/"/,"",$2); gsub(/"/,"",$3); print $1, $2, $3}' file.csv

and got the below output

10.8.70.67  wireless    UTY_07_ISD
10.8.70.69  wireless    RGB_34_FTR

please help in assigning headings to each column.

like image 888
selvan venkatesh Avatar asked Dec 13 '25 03:12

selvan venkatesh


1 Answers

assuming you don't have comma or double quotes in the quoted strings (a big assumption!) it can be as simple as

$ awk -F, 'NR==1 {print "IP","DEVICETYPE","DEVICENAME"} 
                 {gsub(/"/,""); 
                  print $1,$2,$4}' file | column -t

IP          DEVICETYPE  DEVICENAME
10.8.70.67  wireless    UTY_07_ISD
10.8.70.69  wireless    RGB_34_FTR
like image 52
karakfa Avatar answered Dec 14 '25 15:12

karakfa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!