I'm looking for a way to grep things like: i log for E M, i 1 f x i 0, I xi 1, 3 1,. simply based on number of characters.

Nothing in that hypothetical output is longer than three characters. This hypothetical one-liner would look something like, grep -v [:alnum:] > {3}. (Except I just don't know how to write that in grep syntax.)

  • Do you want to match characters like , and . as part of a word?
    – user13742
    Feb 24, 2012 at 17:31

1 Answer 1

grep -o -w '\w\{1,3\}' data

Options are:

  • -o (a GNU extension) prints only matched words
  • -w (an extension from BSD, but now widely supported) matches only whole words.

It matches only words (in grep, \w (a GNU extension) short for standard [[:alnum:]_] (same as [A-Za-z0-9_] in the C locale)) of length from 1 to 3 (specified by {1,3})

  • 1
    thanks. the basic syntax of your answer is what i needed, and i ended up just using grep '\w\{3}' data and the same command with grep -v..
    – ixtmixilix
    Feb 26, 2012 at 21:29
  • I had to use egrep on my side Oct 19, 2022 at 7:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .