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?
You can use
cut to achieve this (using
some-command | cut -c -80
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;
- 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.
Libin Wen suggests that the equivalent
cut -c 1-80 may be better for understanding.