search multiple pattern in file and delete line if pattern matches

前端 未结 5 1580
[愿得一人]
[愿得一人] 2020-12-10 18:25

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

相关标签:
5条回答
  • 2020-12-10 19:04

    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 =)

    0 讨论(0)
  • 2020-12-10 19:05

    Another solution:

     awk '!/10.45.56.84|10.81.51.92/' file
    
    0 讨论(0)
  • 2020-12-10 19:06

    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

    0 讨论(0)
  • 2020-12-10 19:08

    This might work for you (GNU sed):

    sed -r '/10\.(45\.56\.84|81\.51\.92)/d' file
    
    0 讨论(0)
  • 2020-12-10 19:23
    grep -Fv -f <(echo $'10.45.56.84\n10.81.51.92') filename
    
    0 讨论(0)
提交回复
热议问题