Link to home
Start Free TrialLog in
Avatar of mruff
mruff

asked on

unix purge a column in a csv file EXCEPT the first row which contains the header

Dear experts,
I have a CSV file
column1;column2;column3
1;2;3
4;5;6

I want to purge the 2. column, but NOT the header, expected result
column1;column2;column3
1;;3
4;;6

the current command: awk -F";2" 'BEGIN{OFS=";"}{$2="";print}' input.csv>output.csv
purges the 2. columns for all the rows, including the header
but I want the first row being unchanged and only purge the values in the rows containing the data

Many thanks for your solution
Avatar of arnold
arnold
Flag of United States of America image

Is awk the sole option to use?
You have to count lines and check the count is larger than 1 meaning you skipped the header line,

{counter++} ($counter>1)
.......
you can try below:

awk -F';' ' ! /column1/ { print $1 ";" $3 } \
/column1/ { print } ' inout.csv > output.csv
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mruff
mruff

ASKER

thx works perfect