I would like to use grep
or another suitable tool to look for (and print) lines not based on some pattern, but on the length.
Assume I have a file that contains two lines, where
- Line 1: length = 300 characters
- Line 2: length = 120 characters
I am looking for a command that would only output line 2.
Best Answer
exactly 120 characters
With
grep
:With
awk
:from 0 to 120 characters
With
grep
:With
awk
:For strictly less than 120, replace 120 with 119 or
<=
with<
.120 characters or over:
With
grep
:And some more alternatives:
With
awk
:For strictly more than 120, replace 120 with 121 or
>=
with>
.Those assume that the input is valid text properly encoded as per the locale's charmap. If the input contains NUL characters, sequences of bytes that don't form valid characters, lines larger than
LINE_MAX
(in number of bytes), or a non-delimited last line (in the case ofgrep
;awk
would add the missing delimiter), your mileage may vary.If you want to do that filtering based on the number of bytes instead of characters, set the locale to
C
orPOSIX
(LC_ALL=C grep...
).To do the filtering based on number of grapheme clusters instead of characters and if your
grep
supports a-P
option, you can replace theE
withP
above and.
with\X
.Compare:
(that
été
is 3 grapheme clusters, 5 characters, 7 bytes).Not all
grep -P
implementations support\X
. Some only support the UTF-8 multibyte charmap.Note that filtering based on display width is yet another matter, and display width for a given string of characters depends on the display device. See Get the display width of a string of characters for more on that.