HattrickNZ
HattrickNZ

Reputation: 4653

delete certain lines meeting a criteria except line 1 + sed

This deletes lines matching pattern from filename, with -i the output is written to filename

sed -i '/pattern/d' filename 

delete the first 10 lines of a file

sed '1,10d'

How do I combine the 2 to find the pattern in every line after the first line and if found delete that line? This is my attempt at getting the lines I want to apply my search to(the 2nd line to the last line):

tail -n +1 mergedfile.csv | head -n -1

How do I add the sed part to it?

Upvotes: 0

Views: 128

Answers (2)

potong
potong

Reputation: 58430

This might work for you (GNU sed):

sed '1b;/pattern/d' file

or:

sed '1!{/pattern/d}' file

Upvotes: 2

Cyrus
Cyrus

Reputation: 88646

Try this:

sed -i '2,${/pattern/d}' filename

$: last line

Upvotes: 1

Related Questions