Bash – How to “grep” for line length in a given range

bashgrepperlsed

NOTE: This question is the complement of this Q&A: How to "grep" for line length *not* in a given range?


I need to get only the lines from a textfile (a wordlist, separated with newline) that has a length range of minimum or equal than 3 characters, but not longer or equal than 10.

Example:

INPUT:

egyezményét
megkíván
ki
alma
kevesen
meghatározó

OUTPUT:

megkíván
alma
kevesen

Question: How can I do this in bash?

Best Answer

grep -x '.\{3,10\}'

where

  • -x match pattern to whole line
  • . any symbol
  • {3,10} quantify from 3 to 10 times previous symbol (in the case any ones)