Search code examples
grepfindstr

How to find a string with specified length in a text file using findstr or grep command?


I want to find a string of length 8 which starts with the characters "alo", in a text file.

For findstr, I have tried the following command - findstr /R "\<alo" file.txt. This command searches for strings starting with "alo" but cannot search for strings of length 8. For grep, I don't know how to do it.


Solution

  • grep -woE  'alo.{5}' filename
    

    -o is for printing only the match

    -E is to use extended regex

    -w will make the given expression match only whole words

    The number inside the parenthesis specifies the number of character to match after the letters 'alo'