Search code examples
regexlinuxzcatzgrep

How to use zgrep to display all words of a x size from a wordlist?


I want to display all the words from my wordlist who start with a w and are 9 letters long. Yesterday I learnt a bit more on how to use zgrep so I came with :

zgrep '\(^w\)\(^.........$\)' a.gz

But this doesn't work and I think it's because I don't know how to do a AND between the two conditions. I found that it should be (?=expr)(?=expr) but I can't figure out how to build my command then

So how can I build my command using the (?=expr) ?

for example if I have a wordlist like this:

  • Washington
  • Sausage
  • Walalalalalaaaa --> shouldn't match
  • Wwwwwwwww --> should match

Solution

  • You may use

    zgrep '^w[[:alpha:]]\{8\}$' a.gz
    

    The POSIX BRE pattern will match a string that

    • ^w - starts with w
    • [[:alpha:]]\{8\} - then has eight letters
    • $ - followed with with the end of string marker.

    Also, see the 9.3 Basic Regular Expressions.