6 Answers 6
You may be looking for fmt:
fmt file
This pretty aggressively reformats your text, so it may do more than what you want.
Alternatively, the cut command can cut text to a specific column width, discarding text beyond the right margin:
cat file | cut -c1-80
Another handy option is the less -S command, which displays a file in a full screen window with left/right scrolling for long lines:
less -S file
9 Comments
$COLUMNS defined: try cut -c1-$COLUMNS fileless is running using the key combination -SNote that cut accepts a filename as an argument.
This seems to work for me:
watch 'bash -c "cut -c -$COLUMNS file"'
For testing, I added a right margin:
watch 'bash -c "cut -c -$(($COLUMNS-10)) file"'
When I resized my terminal, the truncation was updated to match.
3 Comments
watch ./colscript, I see 80 displayed. When I change the width by dragging the edge of the window (I'm using PuTTY) the number changes to match. What do you see?echo $BASH_VERSION gives me "3.2.48(1)-release" The reason that running ./colscript from the command line echoes a blank line is that $COLUMNS is only populated for interactive shells. You can try added -i at the end of the first line of ./colscript so you have #!/bin/bash -ias stated by others, the answer is cut -c ..., but to add some dynamic to it, I prefer this:
cat file.txt |cut -c -$(tput cols)
1 Comment
to toggle long-line-wrap in less. Default is to wrap.
- `less file`
- in file type `"-S"` to toggle to truncate on line width
- to toggle back `"-S"` again.
1 Comment
less with -S as an option: less -S file or include it in your environment: export LESS="-S" (plus other options). In either case, you can still use the toggle once it's running if you want.The use of cut does not take into account that tabs are considered a single character \t but they are printed as 8 blank spaces. Thus a file with tabs will be cut at different perceived columns.
less -S truncates optimally the text, also in the presence of tabs, but AFAIK it cannot be used to non-interactively print the "chopped" file.
A working solution is to convert tabs into spaces through expand and then cut the output:
expand < file | cut -c -$(tput cols)
1 Comment
expand is a great help; thanks, I was just wrestling with this issue (with tabs). Glad I scrolled to the bottom!Other solution, this time using paginate command pr
echo "your long line" | pr -m -t -w 80