tene
1
I have a file with many rows.
I want to grep for multiple patterns from the file.
For eg:
XX=123|YY=222|ZZ=566
AA=123|EE=222|GG=566
FF=123|RR=222|GG=566
DD=123|RR=222|GG=566
I want the lines which has both XX and ZZ.
I know I can get it like this.
grep XX file | grep YY
But I want to know whether I can do this in a single command.
like we do this
grep "XX|YY" file for grep either XX or YY
skmdu
2
$grep -e "XX" -e "YY" filename
tene
3
Thanks for the quick reply.
Let me try this.
No, this command is wrong:
$ cat urfile
XX=123|YY=222|ZZ=566
AA=123|EE=222|GG=566
FF=123|RR=222|GG=566
DD=123|RR=222|GG=566
XX=123|YY=222|Zx=566
$ grep -e "XX" -e "YY" urfile
XX=123|YY=222|ZZ=566
XX=123|YY=222|Zx=566
Can use awk:
awk -F="|" '/XX/&&/ZZ/' urfile
skmdu
5
@rdcwayx,
Can you please what is wrong in that grep command ?
---------- Post updated at 03:10 AM ---------- Previous update was at 03:09 AM ----------
@rdcwayx,
Can you please tell, what is wrong in that grep command ?
tene
6
btw, I expect the same result.
XX=123|YY=222|ZZ=566
XX=123|YY=222|Zx=566
But I expected some command like yours
awk -F"|" '/XX/&&/ZZ/' urfile
I will try this also.
Thanks
there is nothing technically wrong, just that YY should be ZZ. i think.
---------- Post updated at 04:09 AM ---------- Previous update was at 04:07 AM ----------
shell
while read -r line
do
case "$line" in
*XX*) f=1;;&
*ZZ*)
[ "$f" -eq 1 ] && echo $line;f=0;;
esac
done < "file"
tene
8
@skmdu and @rdcwayx
I tried both ways and got correct results for rdcwayx's command.
skmdu's command gave 'either' kind of results.
rdcwayx' command gave 'both' kind of results.
Thanks all.
awk '/XX/&&/ZZ/' infile
should suffice. No need for the -F thingies.
If one key is always in a different field than the other then this should also work:
grep "XX.*ZZ" infile
rdcwayx
10
Ok, I give the wrong sample to make your confused. Now I explain it again.
The request from tene is : need find out both XX and ZZ in one line.
So, if run your command:
grep -e "XX" -e "ZZ" urfile
XX=123|YY=222|ZZ=566
XX=123|YY=222|Zx=566
you will see the second line is incorrect. You command is same as
grep -E "XX|ZZ" urfile
---------- Post updated at 01:44 PM ---------- Previous update was at 01:42 PM ----------
not really, I also think to use grep by XX.*ZZ at first, but if ZZ is before XX, you will not get the correct answer.
Correct, one has to follow the other, perhaps I did not write that clear enough.