Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I grep for multiple patterns but have some be inverse? [duplicate]

Tags:

grep

bash

shell

awk

I know that I can search for multiple patterns like so

    grep -e 'pattern1|pattern2' file

and I can invert the grep search like so

    grep -v 'pattern' file

but is there a way I can grep for one pattern while simultaneously doing an inverse grep for another?

    grep -e 'pattern I want' -v 'pattern I do not want' file
like image 379
Ryan Schubert Avatar asked Feb 18 '19 20:02

Ryan Schubert


2 Answers

You may use awk as an alternative:

awk '/pattern I want/ && !/pattern I do not want/' file
like image 78
anubhava Avatar answered Nov 08 '22 00:11

anubhava


Assuming suitably quoted patterns,

 sed -n "/$pat1/ { /$pat2/ d; p; }" file

-n tells sed not to print unless explicitly requested.
/$pat1/ { ... } says on lines matching $pat1, execute the commands in the braces.
/$pat2/ d; says on lines with $pat2, delete, which automatically cycles to the next line of input and ignores any more commands for this line, skipping the p. If it does not see $pat2, the d doesn't fire and proceeds to...
p means print the current line.

My example:

$: grep '#if ' dcos-deploy.sh
#if   (( ${isPreProd:-0} ))
#if [[ "$target_mesosphere_cluster" != CDP ]]

$: pat1="^#if "
$: pat2="\\[\\["

$: sed -En "/$pat1/ { /$pat2/ d; p; }" dcos-deploy.sh
#if   (( ${isPreProd:-0} ))
like image 24
Paul Hodges Avatar answered Nov 07 '22 23:11

Paul Hodges