Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can i delete a line if it is longer than e.g.: 2048 chars?

share|improve this question
Do you insist on using sed? This is easy, for example in python. And no doubt even easier in perl. Though the question is not terribly well defined. Copy a file, removing all lines longer than 2048, or something else? – Faheem Mitha Mar 23 '11 at 18:21
up vote 14 down vote accepted
sed '/^.\{2048\}..*/d' input.txt > output.txt
share|improve this answer
I get the error message sed: 1: "/^.\{2048\}..*/d": RE error: invalid repetition count(s) (Mac OS X) – wedi Oct 13 '14 at 15:47
@wedi you probably want to install the GNU version instead of the BSD version that ships with Mac. This is easy with brew – Freedom_Ben Jul 6 at 0:00

Here's a solution which doesn't use extended regular expressions:

sed '/^.\{2048,\}$/d' file.in >file.out
share|improve this answer
I get the error message sed: 1: "/^.\{400,\}$/d": RE error: invalid repetition count(s) (Mac OS X) – wedi Oct 13 '14 at 15:47

Something like this should work in Python.

of = open("orig")
nf = open("new",'w')
for line in of:         
    if len(line) < 2048:
share|improve this answer
Personally, @Faheem, I prefer your answer. The reason why is that it was very easy for me to turn it around into 'delete all lines smaller than x'. I don't use Python all the time, but when I do I always feel I should learn it well. – ixtmixilix May 22 '11 at 18:18
@ixtmixilix: Yes, using a full featured language like Python is pretty flexible. Thanks for the comment. – Faheem Mitha May 24 '11 at 16:46
perl -lne "length < 2048 && print" infile > outfile
share|improve this answer
+1 The -l isn't needed, though. – Joseph R. Jan 29 '14 at 17:22
Does not work for me. Perl v5.16.2. Warning: Use of "length" without parentheses is ambiguous at -e line 1. Unterminated <> operator at -e line 1. – wedi Oct 13 '14 at 15:51
You may try length($_) > 2048 && print. length is a shortcut for length($_) anyway. – MaratC Nov 17 '14 at 12:10

The above answers do not work for me on Mac OS X 10.9.5.

What does work is: sed '/.\{2048\}/d'.

Not asked but for reference: the reverse can be achieved by sed '/.\{2048\}/!d'.

share|improve this answer
lol, but sed: 1: "/.\{2048\}/d": RE error: invalid repetition count(s) (Mac OS X, 10.10.4) – alex gray Jul 24 '15 at 13:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.