Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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
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. – deepmax Jan 7 '13 at 13:49

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.