Solved

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

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - French characters 6 84
Configure Robocopy to excluding folders 6 59
change the windows script file to BAT 10 44
awk file 6 59
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

825 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