user001
user001

Reputation: 991

How to delete lines having specific text using sed command?

I saw a similar Question on this forum but this question is little different from those questions. Hence please do not consider this as a duplicate.

I have a file containing data as

blah blah
blah blah Select from table blah blah QWERTY blah
Select from table
QWERTY Here
blah blah

Now I want to delete all the lines having both Select from table and QWERTY

Expected Output:

blah blah
Select from table
QWERTY Here
blah blah

I tried as below:

sed /*Select from table*QWERTY*/d file.txt

But it is not working. Could anyone please help on this?

Upvotes: 2

Views: 159

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85775

You was close with you sed attempt:

$ sed '/Select from table.*QWERTY/d' file
blah blah
Select from table
QWERTY Here
blah blah

You should always quotes the script with single quotes and don't confuse shell globbing with regular expressions. To match zero or more of any character using regular expressions is .*.


If order is guaranteed then a simply grep will do using the -v option:

$ grep -v 'Select from table.*QWERTY' file
blah blah
Select from table
QWERTY Here
blah blah

If order isn't guaranteed then used a language such as awk where you can combine regular expression matches with logical operators:

$ awk '!/Select from table/||!/QWERTY/' file
blah blah
Select from table
QWERTY Here
blah blah

Upvotes: 3

Related Questions