Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a huge text file. This file has many lines and most of them have more that N characters before end-line.

How can I use sed to truncate lines from Nth character until the end of line?

Note: Obviously after this operation maximum line length is N.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

With sed you can do it like this:

sed 's/\(.\{1,80\}\).*/\1/'

To truncate to 80 columns. It's more readable with ERE (Extended Regular Expressions), enabled with -r in GNU sed:

sed -r 's/(.{1,80}).*/\1/'

But, you could do it more efficiently with cut:

cut -c 1-80
share|improve this answer
    
The solutions work fine. Thanks Thor. –  M M. Jan 7 '13 at 13:49
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.