Reed_Xia
Reed_Xia

Reputation: 1442

How to remove the lines contain certain string using Shell?

I'm using Ubuntu to run an automation test, it need process a text log file, such as following

INVITE INVITE sip:[email protected]:5060 SIP/2.0
INVITE SIP/2.0 100 Trying
1154845 NOTIFY  NOTIFY sip:[email protected]:5065 SIP/2.0
1154845 NOTIFY  NOTIFY SIP/2.0 200 OK
INVITE SIP/2.0 180 Ringing

I want to just remove the 2 lines with "NOTIFY" and get log file below, I'm new to Shell and tried some google but didn't figure out the way, can you please share how to do that? Thanks!

INVITE INVITE sip:[email protected]:5060 SIP/2.0
INVITE SIP/2.0 100 Trying
INVITE SIP/2.0 180 Ringing

Upvotes: 0

Views: 38

Answers (2)

zedfoxus
zedfoxus

Reputation: 37069

The awk way:

awk '!/NOTIFY/ {print $0}' filename

Explanation

awk intends to print the line represented by $0 if the line contains NOTIFY as indicated by /NOTIFY/. Since we don't want to include lines containing NOTIFY, we put a ! before the match like so: !/NOTIFY/

--

The sed attempt:

sed -n '/NOTIFY/!p' filename

Upvotes: 1

Adam Harrison
Adam Harrison

Reputation: 3421

Try the following:

grep -v "NOTIFY" filename.txt

Grep is commonly used to print lines that match a certain pattern, but the "-v" option allows for inverting the match; removing lines that match the pattern.

More information can be found here.

Upvotes: 3

Related Questions