Super User is a question and answer site for computer enthusiasts and power users. 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

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 5 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .