search multiple pattern in file and delete line if pattern matches

删除回忆录丶 提交于 2020-01-10 03:09:06

问题


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



来源:https://stackoverflow.com/questions/12760587/search-multiple-pattern-in-file-and-delete-line-if-pattern-matches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!