'grep search with regex
how to find a line that contains exactly 3 "w", 5 "t" and no "v" with grep?
Input:
----------
aabbccddd4444
wccwwtttjjttuu
zzxxxwwwmmmnnnn
Expected output:
----------------
wccwwtttjjttuu
Because in "wccwwtttjjttuu" we have 3 "w", 5 "t" and no "v"
Thanks!
Solution 1:[1]
$ cat ip.txt
aabbccddd4444
wccwwtttjjttuu
wvccwwtttjjttuu
zzxxxwwwmmmnnnn
ccwwtttjjttuu
with grep
, using extended regex and -x
option match only whole line
$ grep -xE '([^w]*w){3}[^w]*' ip.txt | grep -xE '([^t]*t){5}[^t]*' | grep -v 'v'
wccwwtttjjttuu
with awk
, not sure which versions support {3}
regex construct. I tested on GNU awk 4.1.3
$ awk '/^([^w]*w){3}[^w]*$/ && /^([^t]*t){5}[^t]*$/ && !/v/' ip.txt
wccwwtttjjttuu
with perl
$ perl -ne '(@w)=/w/g; (@t)=/t/g; (@v)=/v/g; print if $#w==2 && $#t==4 && $#v==-1' ip.txt
wccwwtttjjttuu
@w, @t, @v
arrays contains allw, t, v
characters respectively$#w
gives index of last element in@w
, so it's value is number of elements minus one
Solution 2:[2]
perl -ne'/v/||y/w/w/d==3&&y/t/t/d==5&&print'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | mrqiao001 |