让我们来看看它是如何工作的:
$ wc filenamelines words characters filename
输出的是文件的行数、单词数、字符数和文件名。
想获得特定的输出,我们必须使用选项:
-c打印字节总数-l打印行数-w打印单词总数-m打印字符总数
wc 示例
让我们来看看它的运行结果。
让我们从一个文本文件 lormipsm.txt开始。首先,我们通过cat查看文件内容,然后使用wc:
$ cat loremipsm.txtLinux is the best-known and most-used open source operating system.As an operating system, Linux is software that sits underneath all of the other software on a computer,receiving requests from those programs and replaying these requests to the computer's hardware.$ wc loremipsm.txt3 41 268 loremipsm.txt
假设我只想查看文件的字节数:
$ wc -c loremipsm.txt268 loremipsm.txt
查看文件的行数:
$ wc -l loremipsm.txt3 loremipsm.txt
查看文件的单词数:
$ wc -w loremipsm.txt41 loremipsm.txt
现在只查看文件的字符数:
$ wc -m loremipsm.txt268 loremipsm.txt
sed的语法格式如下:
$ sed [OPTION] &lsquoATTERN’ filename
sed 常用的一些选项:
-n取消默认输出p打印指定的数据d删除指定行q退出sed脚本
sed 示例
我们来看看 sed是如何运作的。我们从data文件开始,其中的字段表示编号、名称、年龄和操作系统。
如果行出现在特定的行范围内,该行将打印 2 次:
$ cat data1 Vicky Grant 20 linux2 Nora Burton 19 Mac3 Willis Castillo 21 Windows4 Gilberto Mack 30 Windows5 Aubrey Hayes 17 windows6 Allan Snyder 21 mac7 Freddie Dean 25 linux8 Ralph Martin 19 linux9 Mindy Howard 20 Mac$ sed '3,7 p' data1 Vicky Grant 20 linux2 Nora Burton 19 Mac3 Willis Castillo 21 Windows3 Willis Castillo 21 Windows4 Gilberto Mack 30 Windows4 Gilberto Mack 30 Windows5 Aubrey Hayes 17 windows5 Aubrey Hayes 17 windows6 Allan Snyder 21 mac6 Allan Snyder 21 mac7 Freddie Dean 25 linux7 Freddie Dean 25 linux8 Ralph Martin 19 linux9 Mindy Howard 20 Mac
这里的操作用单引号括起来,表示第 3 行和第 7 行,并且使用了 p打印出符合匹配规则的数据。sed 的默认行为是在解析后打印每一行。这意味着由于使用了p,第 3 行到第 7 行打印了两次。
如何打印文件中特定的行?使用 -n选项来消除在输出中不匹配的行:
$ sed -n '3,7 p' data3 Willis Castillo 21 Windows4 Gilberto Mack 30 Windows5 Aubrey Hayes 17 windows6 Allan Snyder 21 mac7 Freddie Dean 25 linux
使用 ‘-n’ 仅仅只有第 3 行到第 7 行会被打印。
省略文件中的特定行。使用 d从输出中删除行:
$ sed '3 d' data1 Vicky Grant 20 linux2 Nora Burton 19 Mac4 Gilberto Mack 30 Windows5 Aubrey Hayes 17 windows6 Allan Snyder 21 mac7 Freddie Dean 25 linux8 Ralph Martin 19 linux9 Mindy Howard 20 Mac$ sed '5,9 d' data1 Vicky Grant 20 linux2 Nora Burton 19 Mac3 Willis Castillo 21 Windows4 Gilberto Mack 30 Windows
从文件中搜索特定的关键字:
$ sed -n '/linux/ p' data7 Freddie Dean 25 linux8 Ralph Martin 19 linux$ sed -n '/linux/I p' data1 Vicky Grant 20 Linux7 Freddie Dean 25 linux8 Ralph Martin 19 linux
在这些例子中,我们在 / /中使用了一个正则表达式。如果文件中有类似的单词,但大小写不一致,可以使用I使得搜索不区分大小写。回想一下,-n删除了输出中不匹配的行。
替换文件中的单词:
$ sed 's/linux/linus/' data1 Vicky Grant 20 Linux2 Nora Burton 19 Mac3 Willis Castillo 21 Windows4 Gilberto Mack 30 Windows5 Aubrey Hayes 17 windows6 Allan Snyder 21 mac7 Freddie Dean 25 linus8 Ralph Martin 19 linus9 Mindy Howard 20 Mac
这里 s/ / /表示它是一个正则表达式。在两个/之间的就是定位的单词和需要替换的新单词。
现在让我们使用 tr命令将letter文件中的所有小写字符转换为大写字符:
$ cat letterLinux is too easy to learn,And you should try it too.$ cat letter | tr 'a-z' 'A-Z'LINUX IS TOO EASY TO LEARN,AND YOU SHOULD TRY IT TOO.
这里的 a-z、A-Z表示我们想要将a到z范围内的小写字符转换为大写字符。
删除文件中的 o字符:
$ cat letter | tr -d 'o'Linux is t easy t learn,And yu shuld try it t.
从文件中压缩字符 o意味着如果o在文件中重复出现,那么它将会被删除并且只打印一次:
$ cat letter | tr -s 'o'Linux is to easy to learn,And you should try it to.