当前位置: 首页 > news >正文

Linux shell编程学习笔记85:fold命令——让文件瘦身塑形显示

0 引言

我们使用的电脑屏幕有宽有窄,我们有时候希望文件能按照我们的屏幕宽度来调整和匹配,这时我们可以使用fold命令。

1 fold命令 的帮助信息、功能、命令格式、选项和参数说明

1.1 fold 命令 的帮助信息

我们可以输入命令 fold--help 来查看fold 命令的帮助信息。

[purpleendurer @ bash ~ ] fold --help
Usage: fold [OPTION]... [FILE]...
Wrap input lines in each FILE (standard input by default), writing to
standard output.Mandatory arguments to long options are mandatory for short options too.-b, --bytes         count bytes rather than columns-c, --characters    count characters rather than columns-s, --spaces        break at spaces-w, --width=WIDTH   use WIDTH columns instead of 80--help     display this help and exit--version  output version information and exitGNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report fold translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'fold invocation'
[purpleendurer @ bash ~ ] 

 

 1.2 fold命令 的功能

fold命令用于限制文件内容列宽。

fold命令会从指定的文件里读取内容,将超过限定列宽的列加入增列字符后,输出到标准输出设备。

1.3 fold命令 格式

fold [选项]... [文件]...

1.4 fold 命令 选项说明

选项说明
-b,--bytes计算字节而不是列
-c,--characters计算字符数而不是列数
-s,--spaces--在空格处断开
-w,--width=WIDTH使用 WIDTH 列而不是 默认的80 列
--help显示此帮助并退出
--version输出版本信息并退出

1.5 参数说明

文件:要处理的文件名称。

若不指定任何文件名称,或是所给予的文件名为"-",则fold指令会从标准输入设备读取数据。

2 fold 命令使用实例

2.0  创建演示文件

我们使用echo命令成生序列 0-9、A-Z和a-z通过输出重定向保存到文件a 

[purpleendurer @ bash ~ ] echo {0..9} > a
[purpleendurer @ bash ~ ] echo {A..Z} >> a
[purpleendurer @ bash ~ ] echo {a..z} >> a
[purpleendurer @ bash ~ ] cat a
0 1 2 3 4 5 6 7 8 9
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
[purpleendurer @ bash ~ ] 

2.1 按指定列数换行输出

在默认情况下,fold 命令将列数限制为 80。

我们可以使用 -w 选项调整列数值。

例如以7列输出文件a的内容:

[purpleendurer @ bash ~ ] fold -w 7 a
0 1 2 34 5 6 
7 8 9
A B C DE F G 
H I J KL M N 
O P Q RS T U 
V W X YZ
a b c de f g 
h i j kl m n 
o p q rs t u 
v w x yz
[purpleendurer @ bash ~ ] 

 2.2 在空格处进行折断

为了避免行末的单词因为列宽限制而断开成两行,我们可使用 -s选项确保行仅在空格处换行。

如以7列输出文件a的内容,并确保行仅在空格处换行。

[purpleendurer @ bash ~ ] fold -w 7 -s a
0 1 2 
3 4 5 
6 7 8 9
A B C 
D E F 
G H I 
J K L 
M N O 
P Q R 
S T U 
V W X 
Y Z
a b c 
d e f 
g h i 
j k l 
m n o 
p q r 
s t u 
v w x 
y z
[purpleendurer @ bash ~ ] 

2.3 按指定字节数换行输出

 如果我们希望fold 计量单位是字节数而不是列数,可以使用 -b 选项。

例如,以20个字节换行输出文件a的内容

[purpleendurer @ bash ~ ] fold -b20 a
0 1 2 3 4 5 6 7 8 9
A B C D E F G H I J 
K L M N O P Q R S T 
U V W X Y Z
a b c d e f g h i j 
k l m n o p q r s t 
u v w x y z
[purpleendurer @ bash ~ ] 


http://www.mrgr.cn/news/42251.html

相关文章:

  • 认知杂谈95《君子藏器于身,待时而动》》
  • 探索蛋白质相互作用的新视角:图神经网络在预测中的应用
  • day03 笔试练习
  • SpringBoot整合QQ邮箱
  • 酒店生态发展旅游四个一体化建设-—未来之窗行业应用跨平台架构
  • 状态码(204)的使用场景
  • OSDU轻量化单机部署
  • 【CKA】十一、Pod封装多个容器
  • 《C++音频降噪秘籍:让声音纯净如初》
  • std::set
  • vue 不是spa 单页面应用吗? 配置路由工作模式为history 后 ,为什么配置Nginx的 try_files 可以根据url 找到对应的文件?
  • 毕业设计选题:基于ssm+vue+uniapp的电子点餐系统小程序
  • 信息安全工程师(32)认证技术方法
  • 拉普拉斯频域滤波器与高斯高通滤波器等价
  • 信息安全工程师(31)认证类型与认证过程
  • 【社保通-注册安全分析报告-滑动验证加载不正常导致安全隐患】
  • vue-cli老项目继续优化:json压缩神器 compress-json
  • 【ShuQiHere】深入理解 LC-3 的输入输出机制(I/O Mechanism)
  • C 语言预处理详解:从宏替换到条件编译
  • SSY20241002提高组T4题解__纯数论