43

In *nix, how do I display (cat) a file with no line-wrapping? Longer lines should be cut such that they fit into the screen width.

moo
2,3662 gold badges23 silver badges37 bronze badges
asked Oct 23, 2009 at 23:20

6 Answers 6

60

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
gilad905
3,2473 gold badges20 silver badges25 bronze badges
answered Oct 23, 2009 at 23:23
Sign up to request clarification or add additional context in comments.

9 Comments

Indeed, it does more than I want: it concatenates lines.
Actually, I'm watching the content of the file using watch "cat file" so I can't use less. cat file | cut -c1-80 did the trick, partially. Any way to adjust the cut to the screen size?
You may have the environment variable $COLUMNS defined: try cut -c1-$COLUMNS file
Cool. The "less -S" version is pretty awesome.
You can toggle wrapping while less is running using the key combination -S
|
24

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.

answered Oct 24, 2009 at 2:13

3 Comments

When I run your script 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?
I see nothing, but I figured out why: different bash versions. watch ./colscript works on bash 4.0 (my computer), but not on bash 3.0 (a remote computer). ./colscript doesn't work on any of the two versions.
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 -i
9

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)

answered Apr 5, 2015 at 18:10

1 Comment

this limits the output to a specified (but dynamic) columns length
4

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.
answered Jan 25, 2016 at 18:32

1 Comment

You can also call 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.
3

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)

answered Dec 6, 2016 at 10:45

1 Comment

expand is a great help; thanks, I was just wrestling with this issue (with tabs). Glad I scrolled to the bottom!
0

Other solution, this time using paginate command pr

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

answered Sep 2, 2022 at 7:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.