?
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
?
80 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 79

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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

770 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