0

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.

Kam
  • 25

2 Answers2

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 '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
glenn jackman
  • 85,964