Unix 简明教程
Unix / Linux - Pipes and Filters
在本章中,我们将详细讨论 Unix 中的管道和过滤器。您可以将两个命令连接在一起,这样来自一个程序的输出就成为下一个程序的输入。以这种方式连接的两个或更多个命令形成一个管道。
In this chapter, we will discuss in detail about pipes and filters in Unix. You can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe.
要创建一个管道,请在两个命令之间的命令行上放置一个垂直线 ( | )。
To make a pipe, put a vertical bar (|) on the command line between two commands.
当一个程序从另一个程序获取其输入时,它也会对该输入执行某些操作,并将结果写入标准输出。它被称为 filter 。
When a program takes its input from another program, it performs some operation on that input, and writes the result to the standard output. It is referred to as a filter.
The grep Command
grep 命令搜索一个或多个文件中包含特定模式的行。语法如下 −
The grep command searches a file or files for lines that have a certain pattern. The syntax is −
$grep pattern file(s)
名称 "grep" 来自 ed(Unix 行编辑器)命令 g/re/p ,它表示“全局搜索正则表达式,并打印包含该正则表达式的所有行”。
The name "grep" comes from the ed (a Unix line editor) command g/re/p which means “globally search for a regular expression and print all lines containing it”.
正则表达式不是普通文本(例如,一个单词)就是用来模式匹配的特殊字符。
A regular expression is either some plain text (a word, for example) and/or special characters used for pattern matching.
grep 最简单的用法是查找由单个单词组成的模式。它可以在管道中使用,这样一来只有包含给定字符串的输入文件中的那些行才会发送到标准输出。如果你不给 grep 一个文件名来读取,它会读取其标准输入;所有过滤器程序都采用这种工作方式 −
The simplest use of grep is to look for a pattern consisting of a single word. It can be used in a pipe so that only those lines of the input files containing a given string are sent to the standard output. If you don’t give grep a filename to read, it reads its standard input; that’s the way all filter programs work −
$ls -l | grep "Aug"
-rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$
有各种选项可以与 grep 命令一同使用 −
There are various options which you can use along with the grep command −
Sr.No. |
Option & Description |
1 |
-v Prints all lines that do not match pattern. |
2 |
-n Prints the matched line and its line number. |
3 |
-l Prints only the names of files with matching lines (letter "l") |
4 |
-c Prints only the count of matching lines. |
5 |
-i Matches either upper or lowercase. |
现在,让我们使用一个正则表达式,告诉 grep 查找带有 "carol" 的行,然后是正则表达式中缩写为“.*”的零个或其他字符,然后是“Aug”。−
Let us now use a regular expression that tells grep to find lines with "carol", followed by zero or other characters abbreviated in a regular expression as ".*"), then followed by "Aug".−
在这里,我们使用 -i 选项执行不区分大小写的搜索 −
Here, we are using the -i option to have case insensitive search −
$ls -l | grep -i "carol.*aug"
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$
The sort Command
sort 命令按字母或数字顺序排列文本行。以下示例对 food 文件中的行进行分类 −
The sort command arranges lines of text alphabetically or numerically. The following example sorts the lines in the food file −
$sort food
Afghani Cuisine
Bangkok Wok
Big Apple Deli
Isle of Java
Mandalay
Sushi and Sashimi
Sweet Tooth
Tio Pepe's Peppers
$
默认情况下, sort 命令按字母顺序排列文本行。有许多选项控制分类 −
The sort command arranges lines of text alphabetically by default. There are many options that control the sorting −
Sr.No. |
Description |
1 |
-n Sorts numerically (example: 10 will sort after 2), ignores blanks and tabs. |
2 |
-r Reverses the order of sort. |
3 |
-f Sorts upper and lowercase together. |
4 |
+x Ignores first x fields when sorting. |
可以将两个以上命令链接到管道中。以使用 grep 时先前的管道为例,我们可以进一步按大小对 8 月修改的文件进行分类。
More than two commands may be linked up into a pipe. Taking a previous pipe example using grep, we can further sort the files modified in August by the order of size.
以下管道包括命令 ls 、 grep 和 sort −
The following pipe consists of the commands ls, grep, and sort −
$ls -l | grep "Aug" | sort +4n
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02
$
此管道按大小对 8 月修改的目录中所有文件进行分类,并将它们打印在终端屏幕上。排序选项 +4n 跳过 4 个字段(字段由空格分隔),然后按数字顺序对行进行排序。
This pipe sorts all files in your directory modified in August by the order of size, and prints them on the terminal screen. The sort option +4n skips four fields (fields are separated by blanks) then sorts the lines in numeric order.
The pg and more Commands
通常您可以将长输出在屏幕上压缩,但如果您通过更多或将 pg 命令用作过滤器来运行文本;当屏幕满载文本后,显示就停止。
A long output can normally be zipped by you on the screen, but if you run text through more or use the pg command as a filter; the display stops once the screen is full of text.
假设您有一个长目录列表。要使读取排序的列表更容易,将输出通过 more 进行管道处理,如下所示 −
Let’s assume that you have a long directory listing. To make it easier to read the sorted listing, pipe the output through more as follows −
$ls -l | grep "Aug" | sort +4n | more
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-r-- 1 john doc 14827 Aug 9 12:40 ch03
.
.
.
-rw-rw-rw- 1 john doc 16867 Aug 6 15:56 ch05
--More--(74%)
当屏幕满载由文件大小顺序排序的行组成的文本时,屏幕将填满。屏幕底部是 more 提示符,您可以在其中键入命令以移动已排序的文本。
The screen will fill up once the screen is full of text consisting of lines sorted by the order of the file size. At the bottom of the screen is the more prompt, where you can type a command to move through the sorted text.
完成后,您可以使用 more 程序的讨论中列出的任何命令。
Once you’re done with this screen, you can use any of the commands listed in the discussion of the more program.