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="";prin t}' 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
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="";prin
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
you can try below:
awk -F';' ' ! /column1/ { print $1 ";" $3 } \
/column1/ { print } ' inout.csv > output.csv
awk -F';' ' ! /column1/ { print $1 ";" $3 } \
/column1/ { print } ' inout.csv > output.csv
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx works perfect
You have to count lines and check the count is larger than 1 meaning you skipped the header line,
{counter++} ($counter>1)
.......