Solved

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

Posted on 2016-11-23
4
41 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 77

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

896 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

15 Experts available now in Live!

Get 1:1 Help Now