I would like to keep the lines with exactly 639 characters in my .txt file. What is the command to do so?
4 Answers
You can use grep
:
grep -E '^.{639}$' your.txt
The ^
and $
match beginning and end of line. The .{639}
match any character exactly 639 times.
As Stéphane commented, this can be shortened (by one character) to:
grep -Ex '.{639}' your.txt
with -x
indicating: Select only those matches that exactly match the whole line
-
3@Student If this solves your problem, please consider accepting this answer by clicking the ✔ (checkmark) next to the answer. That is the way other people know your problem has been solved. If a better answer comes along, you can always change the accepted answer.– AnthonApr 18, 2016 at 11:28
-
2
-
3Never bury in obscure, tool dependant options what can be expressed in standard language features. Apr 18, 2016 at 22:08
-
@user1663987,
-x
is specified in POSIX; if that's "obscure" to you, you would be well advised to study through the entirety ofman man
and learn to use it. ;)– WildcardApr 19, 2016 at 8:07 -
It can be obscure if it is a seldom used option which implements commonly known operators. Apr 19, 2016 at 13:10
using awk:
awk 'length == 639'
or slightly more understandable:
awk 'length() == 639'
\thanks{@fedorqui}
-
1From
man awk
: There is one feature of historical AWK implementations that gawk supports: It is possible to call thelength()
built-in function not only with no argument, but even without parentheses! But then, Using this feature is poor practice, and gawk issues a warning about its use if --lint is specified on the command line. Even though it works, to me, this is quite misleading.– fedorquiApr 18, 2016 at 14:05 -
2
-
2
You can use Vim in Ex mode:
ex -sc 'v/\v^.{639}$/d' -cx file.txt
\v
turn on magic^.{639}$
find lines of exactly 639 charactersv
invert selectiond
deletex
save and close
You haven't said what your data is, or whether its acceptable to break up words in the middle. So here's an alternative that may suit written text better.
export MANWIDTH=639
cat file.txt | man -l -
This will use your default pager to display, or you pipe the output to something else.