Linux Learning Materials-pipeline command (pipe)

Source: Internet
Author: User

Pipeline command (pipe)

As we said earlier, the output data will appear when the bash command executes! So if this group of data must go through a few formalities to get the format we want, how should we set it? This involves a problem with the pipeline command (pipe), and the pipeline command uses the "| "This defining symbol!" In addition, the pipeline command and the "continuous command" is not the same yo! We'll explain that at the bottom of this. Let's start with an example to illustrate a simple pipeline command.

Suppose we want to read the last command, what is the "number" of the root login? Watch out! We only need the "number of times". So the steps that I take are:

Execute last, and all the people in this month will be logged into the data to take out;

Use grep to extract the root of the above output data (stdout), and others do not;

Use the WC This can count the number of rows of instructions to calculate the number of rows in the previous step of the data!

Since the last output is a one-line login, so long as the calculation of a few lines is the meaning of the login several times, so! Through the above three steps, the last data is gradually filtered, you can get our data! The entire command can be written as follows:

[Test @test bin]# Last

[Test @test bin]# Last | grep root

[Test @test bin]# Last | grep root | Wc-l

You can perform "last" separately and then gradually increase to "last | grep root "and finally to the top row, then you can clearly see why this is done!" This Pipeline command "| "You can only process the correct information from the previous command, that is, the standard output (STDOUT) information, for Stdandard error does not directly deal with the ability, please remember." Then the overall pipeline command can be used to represent:


The parts of each pipeline are "instructions"! The input of the latter command comes from the output of the previous command! Let's talk about some basic pipeline command instructions:


--------------------------------------------------------------------------------


Cut

Syntax: [Root @test/root]# cut-d "separator character" [-CF] Fields

Parameter description:

-D: followed by the character to be delimited, the preset is "whitespace"

-C: followed by "Number of characters"

-F: What is the number of blocks followed?

Example:

[Root @test/root]# cat/etc/passwd | Cut-d ":"-F 1

Will be passwd in this file, each line inside: used as a separator number,

and list the first chunk! That's where the name is!

[Root @test/root]# Last | Cut-d ""-f1

Separated by space characters, and the first range is listed!


[Root @test/root]# Last | Cut-c1-20

Remove the last data, 1-20 characters from each line!

Description

This cut is very useful! However, seriously, unless you often analyze the log file, there is not much chance of using cut! All right! The main purpose of cut is to decompose the data in the same row! , most commonly used when analyzing some data or text data! This is because sometimes we use some characters as split parameters, and then we cut the data to get the data we need. I also use this function very often! Especially when it comes to analyzing log files!


--------------------------------------------------------------------------------


Sort

Syntax: [root @test/root]# sort [-t delimiter] [(+ Start) (-end)] [-NRU]

Parameter description:

-T separators: use separators to separate different intervals, and the default is tab

+start-end: Sorted by start range to end interval

-N: Sort with "pure number" (otherwise it will be sorted in text form)

-R: Reverse Sort

-U: The same line that appears, listed only once!

Example:

[Root @test/root]# cat/etc/passwd | Sort

Sort out the listed personal accounts!

[Root @test/root]# cat/etc/passwd | SORT-T: +2n

The personal account is sorted by the user ID (separated by:, the third is the ID,

But the first code is 0)


[Root @test/root]# cat/etc/passwd | SORT-T: +2NR

Reversed-phase sequencing!

Description

Sort is also a very common instruction! Because we often need to compare some information! Give me a second example above. Today, suppose you have a lot of accounts, and you want to know which number the largest user ID is now! Oh! Use sort to get an answer! Of course, its use is more than this! If you have time, you might as well play!


--------------------------------------------------------------------------------


Wc

Syntax: [Root @test/root]# WC [-LMW]

Parameter description:

-L: How many lines

-M: How many characters

-W: how many words?

Example:

[Root @test/root]# cat/etc/passwd | Wc-l

How many lines are there in this file?

[Root @test/root]# cat/etc/passwd | Wc-w

How many words are there in this file!?

Description

Can WC also be used as an instruction? Oh! This is not the WC in the bathroom! This is quite useful for computing the contents of a tool set of files Oh! For example, when you need to know how many accounts are currently in your account file, use the wc-l above! Because/ETC/PASSWD is a representative of the user! So knowing the number of rows will know how many accounts are in the inside! And if you want to calculate how many characters are in a file, hehe! Just use the WC-W parameter!


--------------------------------------------------------------------------------


Uniq

Syntax: [Root @test/root]# uniq

Parameter description:

Example:

[Root @test/root]# Last | Cut-d ""-f1 | Sort | Uniq

Description

This command is used to delete "duplicate rows only show one", for example, you need to know who logged in to your host this month, but do not care about his login, then use the above example, (1) All the data is listed first, (2) The name of the person separately, (3) sorted; (4) Show only one! Since this instruction is to reduce the duplication of things, it certainly needs to be handled with "sorted files"!


--------------------------------------------------------------------------------


Tee

Syntax: [Root @test/root]# Last | Tee Last.list | Cut-d ""-f1

Parameter description:

Example:

[Root @test/root]# Last | Tee Last.list | Cut-d ""-f1

Description

Have you found that when the command is redirected, if we want to send the data to the file, there will be no data on the screen! So what if we need to display the data simultaneously on the screen and in the file? Oh! This is the time you need the tee command! Use last to see the login data for this month, and after using tee, the data will be passed to the next command to execute, and the data will be written to the Last.list file! It's also a good helper!


--------------------------------------------------------------------------------


Tr

Syntax: [Root @test/root]# tr [-ds] SET1

Parameter description:

-D: Delete the SET1 string

-S: Replace duplicate characters!

Example:

[Root @test/root]# Last | TR ' [A-z] ' [A-z] ' <== lowercase to uppercase

[Root @test/root]# cat/etc/passwd | tr-d: <== Hey! : This symbol is missing in/etc/passwd!

[Root @test/root]# Cat/home/test/dostxt | Tr-d ' \ r ' > Dostxt-nom <== to remove the symbol ^m the word end of the DOS file!

Description

In fact, this command can also be written in the "formal notation" inside! Because he is also in the form of formal notation to replace the data! In the example above, using [] can set a string of words! It's also often used to replace weird symbols in files! For example, in the third example above, you can remove the ^m of a DOS file. This thing is quite useful! Believe it's the most troublesome thing to do with Linux & Windows systems! That is, the DOS will automatically add the ^m line at the end of each line! This time we can use this TR to remove the ^m! ^m can use \ r to replace it!


--------------------------------------------------------------------------------


Split

Syntax: [Root @test/root]# split [-BL] input Archive output file leading character

Parameter description:

-B: The file size to divide

-L: divided by the number of rows

Example:

[Root @test/root]# split-l 5/etc/passwd test <== will produce Testaa, Testab, Testac ... Wait, file.

Description

In the case of Windows, what do you need to do to split the file?! It's a headache! Oh! It's a lot easier under Linux! If you want to partition the file, then use the-B size to limit the size of a split file, and if it is the number of rows, then use the-L line to split it! Good use of it! In this way, you can easily divide your file into floppy size, so you copy it!

Pipeline commands are quite important in bash's continuous handlers! In addition, in the analysis of log file is also a very important link, so please pay special attention! All right, what's up?


Linux Learning Materials-pipeline command (pipe)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.