简体   繁体   中英

Bash - pipe multiple grep and print output

I am writing a shell script which will grep a document for certain words and then displaying the found words in colour output.

echo $(egrep -wi --color=always 'error|exception' $logFile)

now I want to combine this grep with another one to exclude a few results For this I want to pipe above command to a grep command to exclude certain patterns

grep -vi '<status>error</status>'

For some reason this fails when I try to execute the command

echo $(egrep -wi --color=always 'error|exception' $logFile | $(grep -v '<STATUS>ERROR</STATUS>') )

or even if I try

echo $(egrep -wi --color=always 'error|exception' $logFile | grep -v '<STATUS>ERROR</STATUS>')

What am I doing wrong? Why is this failing?

The problem seems append only with egrep, --color=always, and -i.

egrep -wi --color=always 'error|exception' /tmp/log.log | grep -v '<STATUS>ERROR</STATUS>'  

doesn't work but

egrep -w --color=always 'error|exception' /tmp/log.log | grep -v '<STATUS>ERROR</STATUS>'

and

egrep -wi --color 'error|exception' /tmp/log.log | grep -v '<STATUS>ERROR</STATUS>'

and

grep -wi --color=always 'error|exception' /tmp/log.log | grep -v '<STATUS>ERROR</STATUS>'

does... But I don't know why your solution does'nt work...

In shell script:

result=`grep -wi --color=always 'error|exception' /tmp/log.log | grep -v '<STATUS>ERROR</STATUS>'`
echo $result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM