Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

5 Answers 5

up vote 11 down vote accepted
sed '/^.\{2048\}..*/d' input.txt > output.txt
share|improve this answer
1  
I get the error message sed: 1: "/^.\{2048\}..*/d": RE error: invalid repetition count(s) (Mac OS X) –  wedi Oct 13 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:
        nf.write(line)
of.close()
nf.close()
share|improve this answer
1  
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

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 at 15:47
perl -lne "length < 2048 && print" infile > outfile
share|improve this answer
    
+1 The -l isn't needed, though. –  Joseph R. Jan 29 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 at 15:51
    
You may try length($_) > 2048 && print. length is a shortcut for length($_) anyway. –  MaratC Nov 17 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

Your Answer

 
discard

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.