In *nix, how do I display (cat) a file with no line-wrapping: longer lines should be cut such that they fit into screen's width.
相关问题
- How to get the return code of a shell script in lu
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Invoking Mirth Connect CLI with Powershell script
- Error building gcc 4.8.3 from source: libstdc++.so
to toggle long-line-wrap in less. Default is to wrap.
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 thencut
the output:expand < file | cut -c -$(tput cols)
as 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)
Note that
cut
accepts a filename as an argument.This seems to work for me:
For testing, I added a right margin:
When I resized my terminal, the truncation was updated to match.
You may be looking for
fmt
: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:Another handy option is the
less -S
command, which displays a file in a full screen window with left/right scrolling for long lines: