Solved

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

Posted on 2016-11-23
4
24 Views
Last Modified: 2016-11-24
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
0
Comment
Question by:mruff
4 Comments
 
LVL 76

Expert Comment

by:arnold
ID: 41900133
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)
.......
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 41900156
you can try below:

awk -F';' ' ! /column1/ { print $1 ";" $3 } \
/column1/ { print } ' inout.csv > output.csv
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 41900157
Change the second half of the awk script to
{if (NR > 1){$2=""};print}

Open in new window


Edit: Above done on the phone, so couldn't easily cut and paste! The full line would be:
awk -F";" 'BEGIN{OFS=";"}{if (NR > 1){$2=""};print}' input.csv>output.csv

Open in new window

"NR" here is an internal awk variable that holds the record number (so line 1 is record 1, line 2 is record 2 and so on). If you process more than one file on the command line, use FNR (which is the record number in the current file), since NR counts all of the lines in all of the files being processed in one call to awk.
0
 

Author Closing Comment

by:mruff
ID: 41900523
thx works perfect
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now