deepseefan
deepseefan

Reputation: 3791

remove lines based on file input pattern using sed

I have been trying to solve a simple sed line deletion problem. Looked here and there. It didn't solve my problem. My problem could simply be achieved by using sed -i'{/^1\|^2\|^3/d;}' infile.txt which deletes lines beginning with 1,2 and 3 from the infile.txt.

But what I want instead is to take the starting matching patterns from a file than manually feeding into the stream editor.

E.g: deletePattern

1 
3
2

infile.txt

1 Line here
2 Line here
3 Line here 
4 Line here 

Desired output

4 Line here 

Thank you in advance,

Upvotes: 1

Views: 1789

Answers (4)

anubhava
anubhava

Reputation: 785156

This grep should work:

grep -Fvf deletePattern infile.txt
4 Line here

But this will skip a line if patterns in deletePattern are found anywhere in the 2nd file.

More accurate results can be achieved by using this awk command:

awk 'FILENAME == ARGV[1] && FNR==NR{a[$1];next} !($1 in a)' deletePattern infile.txt
4 Line here

Upvotes: 1

Sato Katsura
Sato Katsura

Reputation: 3086

With GNU sed (for -f -):

sed 's!^[0-9][0-9]*$!/^&[^0-9]/d!' deletePattern | sed -f - infile.txt

The first sed transforms deletePattern into a sed script, then the second sed applies this script.

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84561

Putting together a quick command substitution combined with a character class will allow a relatively short oneliner:

$ sed -e "/^[$( while read -r ch; do a+=$ch; done <pattern.txt; echo "$a" )]/d" infile.txt
4 Line here

Of course, change the -e to -i for actual in-place substitution.

Upvotes: 1

Maroun
Maroun

Reputation: 95968

Try this:

sed '/^[123]/ d' infile.txt

Upvotes: 0

Related Questions