search multiple pattern in file and delete line if

2019-04-12 07:54发布

问题:

For ex, a file contain contents:

10.45.56.84 raj
10.49.31.81 mum
10.49.31.86 mum
10.81.51.92 guj
10.45.56.116 raj
10.45.56.84 raj

I want to search 10.45.56.84 and 10.81.51.92 in the above file and delete line if pattern matches.

Also i want to do this in single command.

回答1:

Another solution:

 awk '!/10.45.56.84|10.81.51.92/' file


回答2:

grep -Fv -f <(echo $'10.45.56.84\n10.81.51.92') filename


回答3:

You could do this:

sed -e '/10[.]45[.]56[.]84/d;/10[.]81[.]51[.]92/d' file

This has two sed "d" delete commands separated by a semicolon. However, they are only executed if they match the respective pattern enclosed between slashes that come before eachcommand.

You can also use grep:

grep -Ev '10[.]45[.]56[.]84|10[.]81[.]51[.]92' file

The "-v" flag tell grep to print only the lines that don't match the pattern, and we use the OR operator "|" to match either pattern. The "-E" flag is used so we don't have to escape the OR operator with a backslash.

In both cases we place the period between brackets because otherwise the period is used as an operator that matches any character. You may place more characters inside a single pair of brackets, and they will be interpreted as to match one of the characters specified.

Hope this helps =)



回答4:

This might work for you (GNU sed):

sed -r '/10\.(45\.56\.84|81\.51\.92)/d' file


回答5:

awk -F " " '{if($1 != "10.45.56.84") if($1 != "10.81.51.92" ) { print $0;}}' inputFile > OutputFile

Explanation : awk : Awk is a scripting language used for manipulating data and generating reports.

" " : its is used for delimiter. if data is separated by | then need to mention like "|"

{if($1 != "10.45.56.84") if($1 != "10.81.51.92" ) { print $0;}}' : pattern(Conditions) and print in outputFile