17

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


How can we grep for lines that have less than 8 or more than 63 characters, none that contain more than eight and less than 63 characters?

So, some acceptable character counts could be...

7 6 5 4 3 2 1 0

...and...

64 65 66 67 ...
5
  • 3
    I know you ask for grep, but you may consider awk, especially if you need further processing, etc (very flexible, and readable): awk '( length($0)<8 ) || ( length($0)>63 )' #default action of a condition is to print the line(s) matching the condition . Or, with less processing on $0: awk '{ l=length($0) ; if (( l<8 ) || l>63 ) { print $0 ;} }' Commented Dec 14, 2015 at 13:19
  • @mikeserv - yeah in looking at this again it probably makes more sense to close towards this Q&A the other as the dup.
    – slm
    Commented Dec 15, 2015 at 4:40
  • @mikeserv - it's generally a pain, I have to merge the answers into one of the Q's and they then get deleted from the original. This is the guidance for doing it: "Questions should be merged when they are 99% identical and it would be beneficial to have all the answers from multiple duplicate questions in one place. This deletes answers, moves them to the target question, and leaves the current question as a stub with a link to its merge target."
    – slm
    Commented Dec 15, 2015 at 4:47
  • what happened to my comment? Commented Dec 15, 2015 at 8:03
  • @DmitryGrigoryev - guess somebody didn't like it. take it from one who's been there - forget about it.
    – mikeserv
    Commented Dec 15, 2015 at 21:46

1 Answer 1

38
grep -xv '.\{8,63\}' <input >output

grep's -x switch denotes a whole line match - which is to say that any pattern matched must define a line from head to tail. doing...

grep -x pattern

...is generally equivalent to...

grep ^pattern$

grep's -v switch negates a pattern's influence on line-selection. generally doing...

grep pattern

...will only select lines that match the pattern, but with a -v negated pattern only those lines that don't match are selected.

...and so...

grep -xv '.\{8,63\}'

...matches all lines which consist from head to tail of anywhere between 8 and 63 characters, and the -v negated selection causes grep only to print everything else.

You must log in to answer this question.

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