Occasionally, we can use two commands together, and the output of one command is used as input to another command. This is called a pipe. In order to build a pipeline, you need to use a vertical line between the two commands (|
)connection.
Pipe is an important communication mechanism between Linux processes; in addition to pipelines, there are process communication mechanisms such as shared memory, message queues, signals, sockets, and so on.
Pipe using vertical lines (|
) Separate the two commands, and the output of the command to the left of the vertical line is used as the input to the command on the right side of the vertical line. Continuous use of vertical lines indicates that the output of the first command will be used as input to the second command, the output of the second command will be used as input to the third command, and so on.
A tool that accepts data, filters (processes or filters), and then outputs it, called a filter.
grep Command
grep is a powerful text search tool that can use regular expressions and return matching lines with the syntax:
$grep pattern file(s)
>;grep”g/re/p command from ed (a line text editor for Linux), g/re/p is an abbreviation for "globally search for a regular expression and print all lines containing it" It is a global search using regular expressions and prints the matching lines.
A regular expression is a string containing a number of special characters, each of which has a special meaning that can be used to match text. See the regular expression tutorial
for more information.
grep can be thought of as a filter. If you don't specify a file to retrieve for grep, it will be read from a standard input device (usually a keyboard); the same is true for other filters.
The simplest use of the grep command is to retrieve text that contains fixed characters.
For example, using the grep command in a pipe allows only lines containing the specified characters to be output to the display:
$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 The macros$
grep command has many options:
The following we use regular expressions to match such lines: contain the characters "carol", then include any number of other characters (including zero), and finally include “Aug”.
Use the -i option for case-insensitive matching:
$ls -l |
Grep -i "carol.*aug"-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros$
sort command
sort command is very useful in Linux, it will The lines in the file are sorted by letter or number. The sort command can get input from a specific file or from stdin.
For example, sort the rows of the foot file:
$sort foodAfghani CuisineBangkok WokBig Apple DeliIsle of JavaMandalaySushi and SashimiSweet ToothTio Pepe's Peppers$
The following options can be used to control the collation:
The following example uses the ls, grep, and sort commands in a pipeline to filter the rows containing “Aug” and sort by file size:
$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$
The above command sorts the files modified in August in the current directory by size; +4n means the 5th Columns are sorted by number size.
pg and more commands
If there are too many files, all the display will be messy. You can use the pg and more commands to display the page, only one screen at a time.
For example, through the pipeline, use the more command to display the files in the directory:
$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% )
As above, only one screen of text is displayed at a time. After the display is full, stop and prompt the percentage of the entire content. Press the space bar to view the next screen. Press b to view the previous screen.
According to foreign media reports, Linux does not seem to be fully loved by people. Operating syste
Introduction to Soft InterruptsInsert processing that can be deferred from the hard interrupt handl
The missing header file ‘security/pam_modules.h’ is part of the libpam development, a P
Using a Linux terminal is more than just entering commands. After learning these b
Talking about the partition problem of Linux system
Loading a simple module on the Linux 2.6.12 kernel
Linux kernel upgrade basic tutorial
Practical tips: What to do when the Linux system "crash"
Shell regular expressions use detailed explanation
Common troubleshooting methods for Linux systems
How to join a Linux server to an AD domain
Linux Network Programming - socket option
Fedora Features - Smolt solves hardware compatibility problems
Alibaba Cloud CentOS how to add IPV6 support
Powerful choice Intel Core and Win7 strong combination
Teach you to apply for Windows Server 2008 serial number
WinXP system browser can not open how to solve?
Win10 Mobile preview version 14393.105 WeChat photos do not show how to do?
How to shield Ctrl+Esc key sequence in XP system
In order to better upgrade Microsoft launched the Windows7 upgrade key package
Linux shutdown command shutdown