cat文件没有换行

时间:2020-11-25 01:06:10

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.

在* nix中,如何显示(cat)没有换行的文件:应该剪切较长的行以使它们适合屏幕的宽度。

5 个解决方案

#1


19  

Note that cut accepts a filename as an argument.

请注意,cut接受文件名作为参数。

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.

当我调整终端大小时,截断更新以匹配。

#2


35  

You may be looking for fmt:

您可能正在寻找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:

或者,cut命令可以将文本剪切为特定的列宽,丢弃右边距之外的文本:

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命令,它在一个全屏窗口中显示一个文件,左/右滚动为长行:

less -S file

#3


5  

as stated by others, the answer is cut -c ..., but to add some dynamic to it, I prefer this:

正如其他人所说,答案是剪切-c ...,但为了增加一些动态,我更喜欢这个:

cat file.txt |cut -c -$(tput cols)

cat file.txt | cut -c - $(tput cols)

#4


2  

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.

#5


2  

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.

剪切的使用不考虑标签被视为单个字符\ t但它们被打印为8个空格。因此,具有标签的文件将在不同的感知列处被剪切。

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.

less -S最佳地截断文本,也存在标签,但AFAIK它不能用于非交互式打印“切碎”文件。

A working solution is to convert tabs into spaces through expand and then cut the output: expand < file | cut -c -$(tput cols)

一个有效的解决方案是通过展开将标签转换为空格,然后剪切输出:expand

#1


19  

Note that cut accepts a filename as an argument.

请注意,cut接受文件名作为参数。

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.

当我调整终端大小时,截断更新以匹配。

#2


35  

You may be looking for fmt:

您可能正在寻找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:

或者,cut命令可以将文本剪切为特定的列宽,丢弃右边距之外的文本:

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命令,它在一个全屏窗口中显示一个文件,左/右滚动为长行:

less -S file

#3


5  

as stated by others, the answer is cut -c ..., but to add some dynamic to it, I prefer this:

正如其他人所说,答案是剪切-c ...,但为了增加一些动态,我更喜欢这个:

cat file.txt |cut -c -$(tput cols)

cat file.txt | cut -c - $(tput cols)

#4


2  

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.

#5


2  

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.

剪切的使用不考虑标签被视为单个字符\ t但它们被打印为8个空格。因此,具有标签的文件将在不同的感知列处被剪切。

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.

less -S最佳地截断文本,也存在标签,但AFAIK它不能用于非交互式打印“切碎”文件。

A working solution is to convert tabs into spaces through expand and then cut the output: expand < file | cut -c -$(tput cols)

一个有效的解决方案是通过展开将标签转换为空格,然后剪切输出:expand