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
add comment

4 Answers

up vote 10 down vote accepted
sed '/^.\{2048\}..*/d' input.txt > output.txt
share|improve this answer
add comment

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

sed '/^.\{2048,\}$/d' file.in >file.out
share|improve this answer
add comment

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
add comment
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
add comment

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.