Given a text file, or the output of a command, how can I truncate it so that every line longer than N characters (usually N=80 in a terminal) gets shorten to N characters maximum?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use cut to achieve this (using N=80 here):
some-command | cut -c -80
or
cut -c -80 some-file.txt
Replace 80 with the number of characters you want to keep.
Multi-bytes characters may not be handled correctly, depending on your implementation.
Also, multi-characters bytes (aka tabs) may be treated as one char (& this question treats this).
Dale Anderson suggests the use of some-command | cut -c -$COLUMNS which truncates to the current terminal width.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0