How to “grep” for line length *not* in a given range?

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 ...

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x