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 "<html>\n\t<head>\n\t\t<title>%s</title>\n
\t</head>\n\t<body>\n\t\t<p>%s</p>\n\t</body>\n</html>\n" "Page Tit
le" "Page Content"
<html>
<head>
<title>Page Title</title>
</head>
<body>
<p>Page Content</p>
</body>
</html>

[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格式

 

發佈了59 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章