How to use awk and grep -v option to exclude several patterns from several lines
1
0
Entering edit mode
12 months ago
otieno43 ▴ 30

I am trying to use a combination of awk and grep to filter several files and to exclude a couple of patterns. I have several options and I just get error.

My commands that have failed are:

awk '/predicted/{flag=1;next}/detected/{flag=0}flag' $i/result_*.csv | grep "yes" | grep -v "dme" "dps" "ame" "aga" "aae" "api" | awk -F "\t" '{print $14"\t"$6}' - | sort -k1 | uniq > $i"-results-filtered.tsv"

The error "grep: dme: No such file or directory" for each of these "dps" "ame" "aga" "aae" "api".

But when when I run with one variable

awk '/predicted/{flag=1;next}/detected/{flag=0}flag' $i/result_*.csv | grep "yes" | grep -v "dme" | awk -F "\t" '{print $14"\t"$6}' - | sort -k1 | uniq > $i"-results-filtered.tsv"

It works well and exclude all the lines that contain "dme"

Again when I try these:

awk '/predicted/{flag=1;next}/detected/{flag=0}flag' $i/result_*.csv | grep "yes" | grep -v "dme,dps,ame,aga,aae,api" | awk -F "\t" '{print $14"\t"$6}' - | sort -k1 | uniq > $i"-results-filtered.tsv"

awk '/predicted/{flag=1;next}/detected/{flag=0}flag' $i/result_*.csv | grep "yes" | grep -v "dme dps ame aga aae api" | awk -F "\t" '{print $14"\t"$6}' - | sort -k1 | uniq > $i"-results-filtered.tsv"

I get results but no filter

Any help please on how I can modify this to get what I want.

Thanks.

AWK GREP • 872 views
ADD COMMENT
2
Entering edit mode
12 months ago
Michael 55k
grep -Fv -e "dme" -e "dps" -e "ame" -e "aga" -e "aae" -e "api"

The -e switch should do the trick.

ADD COMMENT
0
Entering edit mode

Thank you. It worked like a charm

ADD REPLY

Login before adding your answer.

Traffic: 1960 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6