18.格式化输出(快乐的Linux命令行)

1.本章的命令

nl 添加行号
fold 限制文件列宽
fmt 文本格式转换器
pr 准备打印文本
printf  格式化数据输出
groff 文件格式化系统

2.知识点

知识点1:nl 标记和选项有哪些?

\:\:\: 逻辑页页眉开始处
\:\: 逻辑页主体开始处
\: 逻辑页页脚开始处
-b style 把 body 按被要求方式数行,可以是以下方式:

a = 数所有行

t = 数非空行。这是默认设置。

n = 无

pregexp = 只数那些匹配了正则表达式的行

-f style footer 按被要求设置数。默认是无
-h style header 按被要求设置数。默认是
-i number 页面增加量设置为数字。默认是一。
-n format 设置数数的格式,格式可以是:

ln = 左偏,没有前导零。

rn = 右偏,没有前导零。

rz = 右偏,有前导零。

-p 不要在每一个逻辑页面的开始重设页面数。
-s string 每一个行的末尾加字符作分割符号。默认是单个的 tab。
-v number 将每一个逻辑页面的第一行设置成数字。默认是一。
-w width 将行数的宽度设置,默认是六。

知识点2:printf 转换规范组件?

组件 描述
d 将数字格式化为带符号的十进制整数
f 格式化并输出浮点数
o 将整数格式化为八进制数
s 将字符串格式化
x 将整数格式化为十六进制数,必要时使用小写a-f
X 与 x 相同,但变为大写
% 打印 % 符号 (比如,指定 “%%”)
组件 描述
flags 有5种不同的标志:

# – 使用“备用格式”输出。这取决于数据类型。对于o(八进制数)转换,输出以0为前缀.对于x和X(十六进制数)转换,输出分别以0x或0X为前缀。

0–(零) 用零填充输出。这意味着该字段将填充前导零,比如“000380”。

- – (破折号) 左对齐输出。默认情况下,printf右对齐输出。

‘ ’ – (空格) 在正数前空一格。

+ – (加号) 在正数前添加加号。默认情况下,printf 只在负数前添加符号。

width 指定最小字段宽度的数。
.precision 对于浮点数,指定小数点后的精度位数。对于字符串转换,指定要输出的字符数。
变量 格式 结果 备注
380 "%d" 380 简单格式化整数。
380 "%#x" 0x17c 使用“替代格式”标志将整数格式化为十六进制数。
380 "%05d" 00380 用前导零(padding)格式化整数,且最小字段宽度为五个字符。
380 "%05.5f" 380.00000 使用前导零和五位小数位精度格式化数字为浮点数。由于指定的最小字段宽度(5)小于格式化后数字的实际宽度,因此前导零这一命令实际上没有起到作用。
380 "%010.5f" 0380.00000 将最小字段宽度增加到10,前导零现在变得可见。
380 "%+d" +380 使用+标志标记正数。
380 "%-d" 380 使用-标志左对齐
abcdefghijk "%5s" abcedfghijk 用最小字段宽度格式化字符串。
abcdefghijk "%d" abcde 对字符串应用精度,它被从中截断。

3.代码实操

[me@linuxbox ~]$ nl distros.txt | head
注:类似于cat -n

[me@linuxbox ~]$ echo "The quick brown fox jumped over the lazy dog." | fold -w 12
The quick br
own fox jump
ed over the
lazy dog.
注:行宽为12个字符。 如果没有字符设置,默认是80。所以一行12个。

[me@linuxbox ~]$ echo "The quick brown fox jumped over the lazy dog."
| fold -w 12 -s
The quick
brown fox
jumped over
the lazy
dog.
注:-s考虑了单词边界,这样就不会把单词给分解掉。

‘fmt’ reads from the specified FILE arguments (or standard input if
none are given), and writes to standard output.

   By default, blank lines, spaces between words, and indentation are
preserved in the output; successive input lines with different
indentation are not joined; tabs are expanded on input and introduced on
output.
[me@linuxbox ~]$ fmt -w 50 fmt-info.txt | head
'fmt' reads from the specified FILE arguments
(or standard input if
none are given), and writes to standard output.
By default, blank lines, spaces between words,
and indentation are
preserved in the output; successive input lines
with different indentation are not joined; tabs
are expanded on input and introduced on output.
注:设置为50个字符宽。但是有问题的是保留了第一行的缩进,可以通过-c进行纠正。

[me@linuxbox ~]$ cat > fmt-code.txt
# This file contains code with comments.

# This line is a comment.
# Followed by another comment line.
# And another.

This, on the other hand, is a line of code.
And another line of code.
And another.
[me@linuxbox ~]$ fmt -w 50 -p '# ' fmt-code.txt
# This file contains code with comments.

# This line is a comment. Followed by another
# comment line. And another.

This, on the other hand, is a line of code.
And another line of code.
And another.
注;通过-p '#'可以让两个短的注释合并为一个注释。

[me@linuxbox ~]$ pr -l 15 -w 65 distros.txt
2008-12-11 18:27        distros.txt         Page 1


SUSE        10.2     12/07/2006
Fedora      10       11/25/2008
SUSE        11.0     06/19/2008
Ubuntu      8.04     04/24/2008
Fedora      8        11/08/2007


2008-12-11 18:27        distros.txt         Page 2


SUSE        10.3     10/04/2007
Ubuntu      6.10     10/26/2006
Fedora      7        05/31/2007
Ubuntu      7.10     10/18/2007
Ubuntu      7.04     04/19/2007
注:-l 选项(页长)和 -w 选项(页宽)定义了宽65列,长15行的一个“页面”。

[me@linuxbox ~]$ printf "I formatted the string: %s\n" foo
I formatted the string: foo
[me@linuxbox ~]$ printf "I formatted '%s' as a string.\n" foo
I formatted 'foo' as a string.
[me@linuxbox ~]$ printf "%d, %f, %o, %s, %x, %X\n" 380 380 380 380 380 380
380, 380.000000, 574, 380, 17c, 17C
[me@linuxbox ~]$ printf "%s\t%s\t%s\n" str1 str2 str3
str1 str2 str3
[me@linuxbox ~]$ printf "Line: %05d %15.3f Result: %+15d\n" 1071
3.14156295 32589
Line: 01071 3.142 Result: +32589
[me@linuxbox ~]$ printf "\n\t\n\t\t%s\n
\t\n\t\n\t\t

%s

\n\t\n\n" "Page Tit le" "Page Content" Page Title

Page Content

[me@linuxbox ~]$ man ls | head LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... [me@linuxbox ~]$ zcat /usr/share/man/man1/ls.1.gz | groff -mandoc -T ascii | head LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... 注:模拟man格式

 

你可能感兴趣的:(《快乐的Linux命令行》)