I would like to keep the lines with exactly 639 characters in my .txt file. What is the command to do so?

4 Answers 4


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.
    – Anthon
    Apr 18, 2016 at 11:28
  • 2
    Or just grep -Ex '.{639}' Apr 18, 2016 at 13:49
  • 3
    Never bury in obscure, tool dependant options what can be expressed in standard language features.
    – simpleuser
    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 of man man and learn to use it. ;)
    – Wildcard
    Apr 19, 2016 at 8:07
  • It can be obscure if it is a seldom used option which implements commonly known operators.
    – simpleuser
    Apr 19, 2016 at 13:10

using awk:

awk 'length == 639'

or slightly more understandable:

awk 'length() == 639' 


  • 1
    From man awk: There is one feature of historical AWK implementations that gawk supports: It is possible to call the length() 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.
    – fedorqui
    Apr 18, 2016 at 14:05
  • 2
    perhaps awk 'length() == 639' ?
    – JJoao
    Apr 18, 2016 at 14:24
  • 2
    To me, this is way more understandable : )
    – fedorqui
    Apr 18, 2016 at 14:27

You can use Vim in Ex mode:

ex -sc 'v/\v^.{639}$/d' -cx file.txt
  1. \v turn on magic

  2. ^.{639}$ find lines of exactly 639 characters

  3. v invert selection

  4. d delete

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

You must log in to answer this question.

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