25

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?

  • 9
    grep -x '.\{3,10\}' – Costas Feb 12 '15 at 18:22
  • @Costas brilliant, and the same goes for sed as well. Post a complete answer. – orion Feb 12 '15 at 18:26
33
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)
10

using egrep

egrep '^.{3,10}$'

matches from begining to ending of lines for 3 or more character but less than or equal to 10 characters.

0

Using sed:

sed '/^.\{3,10\}$/!d'

Or:

sed -r '/^.{3,4}$/!d'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy