第1页:第18章过滤器:统计和格式化
本页为第18章过滤器:统计和格式化的章节封面,标示本章讲解的行号、wc统计与制表符、行段落格式化。
第2页:本章需要解决的几个问题
- wc的三个基本选项是什么?
- 使用wc时,行的定义是什么?
- 默认制表符位置是什么?
- fold、fmt和pr的区别是什么?
第3页:18.1创建行号
- 语法定义:nl[-vstart][-iincrement][-b a] [-n ln|rn|rz][file…]其中:start:起始行号increment:行号增量-b a:对所有行编号,包括空行-n ln|rn|rz:数字格式控制,ln=左对齐,rn=右对齐,rz=右对齐,有前导0。
第4页:18.1创建行号
- 例子,有文件data,内容如下:First line of text.Second line of text.Third line of text.Fourth line of text.
第5页:18.1创建行号:nl示例
18.1创建行号命令示例:
$nl data1 First line of text.2 Second line of text.3 Third line of text.4 Fourth line of text.$nl-b a data1 First line of text.2 Second line of text.34 Third line of text.5 Fourth line of text.
第6页:18.1创建行号:nl示例
18.1创建行号命令示例:
$nl-v 100-i 100 data100 First line of text.200 Second line of text.300 Third line of text.400 Fourth line of text.
第7页:18.2统计行、单词和字符数量:wc
- 语法定义:wc[-clLw][file…]其中:-c:只统计字符数-l:只统计行数-w:只统计单词数-L:只在某些需要格式化文本的情况下使用
第8页:18.2统计行、单词和字符数量:wc
- 例子,有文件poem,内容如下:There was a young man from Nantucket,Whose girlfriend had told him to
第9页:18.2统计行、单词和字符数量:wc示例
18.2统计行、单词和字符数量:wc命令示例:
$wc poem2 13 71 peom$ wc-l peom2 poem$ wc-wc peom13 71 peom$ echo “There are `who | wc-l` userids logged in.”There are 5 userids logged in.
第10页:18.2统计行、单词和字符数量:wc
- 字符、单词和行-字符是字母、数字、标点符号、空格、制表符、新行字符。-单词是一串连续的字符,用空格、制表符或新行字符分隔。-行是以新行字符结尾的一串字符。
第11页:18.3 Unix使用制表符的方式
- 制表符的功能-在制表位之间跳转。-Unix假定每隔8个字符有一个制表位,因此默认的制表位是1、9、17、25、33。
第12页:Unix使用制表符的方式:制表符的作用
第13页:18.4可视化制表符和空格
- 怎么辨别文件中的制表符和空格-1.使用字处理程序例如:在vi中,使用命令:set list,可以将制表符显示为^I。-2.使用wc-c统计字符个数。
第14页:18.5将制表符转换成空格:expand
- 语法定义:expand[-i][-tsize |-tlist][file…]其中:size:固定宽度制表符的大小list:制表位列表
第15页:18.5将制表符转换成空格:expand
- 例子
- 有文件animals,内容如下:Kittencatpuppydoglambsheepnerdprogrammer
第16页:将制表符转换成空格:expand
- 执行下面命令:$ expand animals效果如下:kitten<Space><Space>catpuppy<Space><Space><Space>doglamb<Space><Space><Space><Space>sheepnerd<Space><Space><Space><Space>programmer
第17页:将空格换成制表符转:unexpand
- 语法定义:unexpand[-a][-tsize |-tlist][file…]其中:size:固定宽度制表符的大小list:制表位列表-a:转换所有可替换空格,默认只转换行首的空格。
第18页:18.7格式化行:fold
- 语法定义:fold[-s][-wwidth][file…]其中:width:行宽度,默认值为80-s:分拆长行的时候不分开单词。
第19页:18.7格式化行:fold
- 例子:有文件alphabet,内容如下:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
第20页:18.7格式化行:fold:执行下面命令
- 执行下面命令:$ fold-w 13 alphebat效果如下:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
第21页:18.8 80字符行
- 80字符行-fold程序默认在80字符出分行-Unix联机手册每行最多80个字符-终端仿真器默认行宽度是80
第22页:18.9格式化段落:fmt
- 语法定义:fmt[-su][-wwidth][file…]其中:width:行宽度,默认值为80-s:分拆长行的时候不分开单词-u:统一单词及空行之间的缩进和空格
第23页:18.9格式化段落:fmt
- 例子:有文件secret-raw,内容如下:As we all know, realsuccess comes slowly andis due to a number of different factors all comingtogetherover a period of years.Although thereis no real shortcut, there is a secret: a secret sopowerful that you can use it to open doors.
第24页:18.9格式化段落:fmt:执行下面命令
- 执行下面命令:$ fmt-w 50 secret-raw效果如下:As we all know, real success comes slowly and isdue to a number of different factors all comingtogether over a period of years.Although there is no real shortcut, there is asecret: a secret so powerful that you can use itto open doors.
第25页:18.9格式化段落:fmt:法国式间距
- 法国式间距(French spacing)-使用-u选项时,统一间距为法国式间距。-法国式间距规定:1.单词之间最多只有一个空格;2.句子末尾最多只有两个空格。
第26页:18.11按页格式化文本:pr
- 语法定义:pr[-dt][+beg[:end]] [-htext][-ln] [-omargin][-Wwidth][file…]其中:beg,end:需要格式化的第1页和最后一页text:标题中间的文本,默认值是文件名margin:左页边距width:输出的宽度
第27页:按页格式化文本:pr:默认执行效果
- 默认执行效果:1.顶端插入一个标题,占用5行。格式如下:上次修改的日期和事件文件名页号例如:2012-10-24 10:30logfilePage 12.左边插入页边距大小的空白;3.底部插入一个页脚,占用5行。
第28页:18.12按列格式化文本:pr
- 语法定义:pr[-mt][-columns][-llines] [-Wwidth][file…]其中:columns:输出列的数量lines:每页的行数width:输出的宽度
第29页:18.12按列格式化文本:pr
- 例子1:某期刊要求分章内容进行分页和分栏,每页48行,分为2栏,对于刚完成的文章article,可以使用如下管道线将其分成两栏:fmt-w 35 article | pr-2-l 48 > article-2cols
第30页:18.12按列格式化文本:pr
- 例子2:有3篇新闻故事,分别保存在文件n1、n2和n3中。希望将这3篇新闻故事分成一个页面上的3列,每列一个故事。$ fmt-w 23 n1 > f1$ fmt-w 23 n2 > f2$ fmt-w 23 n3 > f3$ pr-m f1 f2 f3> formatted-article$ pr-mt f1 f2 f3> formatted-article