0

This question already has an answer here:

I want to get a grep -v command that will remove the rows for that particular string found, without reading into the first row:

orderID,change_timestamp,Condition,State
OD10,2017-04-25 07:21:13.069,A,D
OD11,2017-04-25 07:21:13.069,A,D
orderID,change_timestamp,Condition,State
OD14,2017-04-26 07:21:13.069,A,D
OD15,2017-04-26 07:21:13.069,A,D
orderID,change_timestamp,Condition,State
OD16,2017-04-27 07:21:13.069,A,D
OD17,2017-04-27 07:21:13.069,A,D

Desired:

orderID,change_timestamp,Condition,State
OD10,2017-04-25 07:21:13.069,A,D
OD11,2017-04-25 07:21:13.069,A,D
OD14,2017-04-26 07:21:13.069,A,D
OD15,2017-04-26 07:21:13.069,A,D
OD16,2017-04-27 07:21:13.069,A,D
OD17,2017-04-27 07:21:13.069,A,D

It would need to output to another file without touching the original.

marked as duplicate by Jeff Schaller, Kusalananda, jsbillings, ilkkachu, Archemar Feb 20 at 13:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The duplicate aims to remove the duplicated header lines, if that's your intention. – Jeff Schaller Feb 19 at 18:11
1
$ awk 'NR==1 {print; next;} !/orderID/' input.txt > output.txt

Shorter syntax:

$ awk 'NR==1 || !/orderID/' input.txt > output.txt
  • You guys rock with such quick response!!!!! I end up taking this suggestion from finswimmer (nice name) and it worked as expected. Thanks again everyone! – Kam Feb 19 at 18:14
0

In the shell, you can redirect the file into a compound command where you read the first line, print it, then exclude that text from the rest of the stream:

{ read -r header; echo "$header"; grep -Fv "$header"; } < file

With this method, you don't have do know the contents of the header in order to exclude it.

The equivalent awk would be

awk 'NR==1 {header = $0; print} $0 == header {next} {print}' file

Not the answer you're looking for? Browse other questions tagged or ask your own question.