本章将详细讨论Unix中的管道和过滤器。
管道可以将两个命令连接在一起,使一个程序的输出成为下一个程序的输入。管道的形式是一个放在两个命令之间的竖条(|)。
过滤器接受某个程序的输出,执行一些过滤操作,然后写到标准输出。
grep命令
grep命令用于搜索文件中符合特定模式的字符串。语法是:
$grep pattern file(s)
“grep”这个名称来自ed(一个Unix行编辑器)命令g/re/p,意思是“全局搜索正则表达式并打印包含它的所有行”。
正则表达式是一种文本模式,该模式描述在搜索文本时要匹配的一个或多个字符串。
grep最简单的用法是查找简单的字符串。
下面展示grep作为过滤器,通过管道接受ls命令的输出,然后打印过滤后的输出:
[root@localhost etc]# ls -l | grep sh
drwxr-xr-x. 2 root root 22 2月 3 12:50 bash_completion.d
-rw-r--r--. 1 root root 2853 10月 31 03:48 bashrc
-rw-r--r--. 1 root root 1620 10月 31 03:48 csh.cshrc
-rw-r--r--. 1 root root 866 10月 31 03:48 csh.login
lrwxrwxrwx. 1 root root 56 2月 3 12:49 favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
----------. 1 root root 384 3月 30 21:51 gshadow
----------. 1 root root 374 2月 3 12:57 gshadow-
lrwxrwxrwx. 1 root root 35 2月 3 12:57 localtime -> ../usr/share/zoneinfo/Asia/Shanghai
----------. 1 root root 834 3月 30 21:52 shadow
----------. 1 root root 710 3月 30 21:38 shadow-
-rw-r--r--. 1 root root 44 10月 31 03:48 shells
drwxr-xr-x. 2 root root 225 2月 3 13:21 ssh
grep命令有很多选项:
序号 | 选项 |
---|---|
1 |
-v 打印所有与模式不匹配的行。 |
2 |
-n 打印匹配行及其行号。 |
3 |
-l 只打印具有匹配行的文件名 |
4 |
-c 只打印匹配行数。 |
5 |
-i 匹配时忽略大小写。 |
示例:
[root@localhost dev]# ls -l | grep -i "ttys"
crw-rw----. 1 root dialout 4, 64 4月 1 20:46 ttyS0
crw-rw----. 1 root dialout 4, 65 4月 1 20:46 ttyS1
crw-rw----. 1 root dialout 4, 66 4月 1 20:46 ttyS2
crw-rw----. 1 root dialout 4, 67 4月 1 20:46 ttyS3
sort命令
sort命令可以对文件内容以行为单位根据字母或数字顺序排序。
下面的例子对os文件中的行进行排序:
os文件内容
[root@localhost ~]# cat os
linux
bsd
win10
suse
debian
macos
ios
android
sort 排序
[root@localhost ~]# sort os
android
bsd
debian
ios
linux
macos
suse
win10
sort命令默认情况下按字母顺序排列文本行。有许多选项可以控制排序:
序号 | 选项 |
---|---|
1 |
-n 按数字排序(例如:10将在2之后排序),忽略空格和制表符。 |
2 |
-r 反向排序。 |
3 |
-f 排序时忽略大小写。 |
4 |
-k 以那个字段 (field) 来进行排序。 |
可以用管道连接多个命令。
下面由管道连接命令ls、grep和sort,命令作用是找出8月份的文件,并按日期排序:
[root@localhost etc]# ls -l | grep "8月" | sort -k7n
drwxr-xr-x. 2 root root 6 8月 2 2017 gcrypt
drwxr-xr-x. 2 root root 6 8月 4 2017 chkconfig.d
-rw-r--r--. 1 root root 570 8月 16 2018 my.cnf
-k7
表示第7个字段,字段由空格分隔,n
表示按数字顺序。
more命令
如果命令输出超过一屏,可使用more命令分屏显示,按任意键可滚动屏幕查看更多内容。
[root@localhost etc]# ls -l | more
总用量 1048
-rw-r--r--. 1 root root 16 2月 3 12:57 adjtime
-rw-r--r--. 1 root root 1518 6月 7 2013 aliases
-rw-r--r--. 1 root root 12288 2月 3 13:21 aliases.db
drwxr-xr-x. 2 root root 236 2月 3 12:50 alternatives
-rw-------. 1 root root 541 4月 11 2018 anacrontab
-rw-r--r--. 1 root root 55 10月 30 22:50 asound.conf
drwxr-x---. 3 root root 43 2月 3 12:50 audisp
drwxr-x---. 3 root root 83 2月 3 13:21 audit
drwxr-xr-x. 2 root root 22 2月 3 12:50 bash_completion.d
-rw-r--r--. 1 root root 2853 10月 31 03:48 bashrc
drwxr-xr-x. 2 root root 6 10月 31 07:31 binfmt.d
-rw-r--r--. 1 root root 38 11月 23 21:16 centos-release
-rw-r--r--. 1 root root 51 11月 23 21:16 centos-release-upstream
drwxr-xr-x. 2 root root 6 8月 4 2017 chkconfig.d
-rw-r--r--. 1 root root 1108 4月 13 2018 chrony.conf
-rw-r-----. 1 root chrony 481 9月 15 2017 chrony.keys
drwxr-xr-x. 2 root root 21 2月 3 12:49 cron.d
drwxr-xr-x. 2 root root 42 2月 3 12:50 cron.daily
-rw-------. 1 root root 0 4月 11 2018 cron.deny
drwxr-xr-x. 2 root root 22 6月 10 2014 cron.hourly
drwxr-xr-x. 2 root root 6 6月 10 2014 cron.monthly
--More--