1

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,

1
  • You could read deletepat.txt into a string, and then try that string as the content of a character class in sed. Something like while read -r ch; do a+=$ch; done <deletepat.txt. Then sed -i "{/^[$a]/d;}" infile.txt (note: you will need to test) Commented Jun 29, 2015 at 6:49

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

the grep is an excellent one. awk works but a glitch if the pattern file is empty, it produces empty file. Just try touch deletePattern and use awk. Thank you.
Thanks for your point regarding awk command. I've corrected that as well.
1

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.

2 Comments

It works perfect except when the pattern is an empty file. touch pattern.txt and running sed produces _ unterminated address regex_ error . Thank you.
Glad I could help. And... you got me... I never thought to test it on a blank file. All the different answers just go to show there is always more than one way to skin a cat in shell computing. Pick your favorite cat!
1

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.

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.