Umidjon Urunov Umidjon Urunov - 1 month ago 15
Linux Question

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!

Answer
$ 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 all w, t, v characters respectively
  • $#w gives index of last element in @w, so it's value is number of elements minus one