Alexandru
Alexandru

Reputation: 25810

cat file with no line wrap

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.

Upvotes: 43

Views: 38944

Answers (6)

Kiteloopdesign
Kiteloopdesign

Reputation: 99

Other solution, this time using paginate command pr

echo "your long line" | pr -m -t -w 80

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993353

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

Upvotes: 59

cipper
cipper

Reputation: 287

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)

Upvotes: 3

James Armstrong
James Armstrong

Reputation: 71

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.

Upvotes: 4

Aquarius Power
Aquarius Power

Reputation: 3985

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)

Upvotes: 9

Dennis Williamson
Dennis Williamson

Reputation: 360143

Note 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.

Upvotes: 24

Related Questions