4

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.

11

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.