Command for keeping the lines with a certain length in a file

text processing

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

Best Answer

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