[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2016-11-23
4
Medium Priority
?
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 80

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 20

Accepted Solution

by:
simon3270 earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

656 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