Shell pipe (pipeline) in linux
Shell getting started (6) Shell pipe (pipeline) command
Pipe (pipeline) command use | to define the symbol.
Pipe pipeline command | it can only process information sent from the previous command, that is, standard output. It has no processing capability for standard error output. The overall pipe pipeline command can be expressed as follows:
For example, if you run ls-al | more on the current user, you can see that it is displayed in the form of more, for example:
In addition, pipe has some other commands.
Capture command: cut, grep
Cut
Options and Parameters
-D: followed by separator characters. Used with-f. -F: separate segments based on the-d delimiter. Use-f to get the meaning of the segment. -C: Extracts fixed character ranges in characters
Eg:
[zhang@localhost ~]$ echo "hello world" | cut -d "o" -f 1hell[zhang@localhost ~]$ echo "hello world" | cut -d "o" -f 2 w[zhang@localhost ~]$ echo "hello world" | cut -d "o" -f 3rld[zhang@localhost ~]$ echo "hello world" | cut -c 3l[zhang@localhost ~]$ echo "hello world" | cut -c 5o[zhang@localhost ~]$ echo "hello world" | cut -c 9r
Grep
Grep options and parameters:
-A: Search for data using text files in binary files-c: count the number of times that 'searchstring' is found-I: case-insensitive-n: By the way, the output row number-v: reverse Selection, that is, to display the line without 'search string' content -- color = auto: You can add color to the found keyword. Color has three parameters (auto, always, never)
Eg:
Enter some data in 1.txt, for example:
[zhang@localhost ~]$ cat 1.txt hello worldabcdefg hijklmnopqrst uvwxyzabchelloworldHEllo1hello2world3
[zhang@localhost ~]$ cat 1.txt | grep hellohello worldhellohello2
The searched hello field is red by default. If -- color = never is added, the color can be removed.
Add the-n parameter to output the row number.
[zhang@localhost ~]$ cat 1.txt | grep hello -n1:hello world5:hello21:hello2
Add the-I parameter to ignore case sensitivity.
[zhang@localhost ~]$ cat 1.txt | grep hello -ihello worldhelloHEllo1hello2
| Continuous use
[zhang@localhost ~]$ cat 1.txt | grep o | grep whello worldopqrst uvwxyzworldworld3
Sorting command: sort, wc, uniq
Sort
Options and Parameters
-F: case-insensitive differences-B: Ignore the leading space section-M: sort by month name-n: sort by pure numbers-r: reverse sorting-u: uniq. In the same data, only one row represents the-t: separator. The default value is to use the tab key to separate-k: sort by filed in that interval.
Eg:
Sort by sort:
[zhang@localhost ~]$ cat 1.txt | sortabcabcdefg hijklmnhelloHEllo1hello2hello worldopqrst uvwxyzworldworld3
-R parameter for reverse sorting
[zhang@localhost ~]$ cat 1.txt | sort -rworld3worldopqrst uvwxyzhello worldhello2HEllo1helloabcdefg hijklmnabc
Wc
If we want to know how many lines, words, and characters are in 1.txt. We can use the wc command.
Options and Parameters
-L: the row to be listed today-w: the number of words listed today (English words)-m: The number of characters
Eg:
[zhang@localhost ~]$ wc 1.txt 9 12 79 1.txt[zhang@localhost ~]$ wc 1.txt -l9 1.txt[zhang@localhost ~]$ wc 1.txt -w12 1.txt[zhang@localhost ~]$ wc 1.txt -m79 1.txt
Uniq
Options and Parameters
-I: Ignore case-c: Count
[zhang@localhost ~]$ cat 2.txt helloHelloWOrldabcabcABChello1
After performing sort on 2.txt, perform uniq.
[zhang@localhost ~]$ cat 2.txt | sort | uniqabcABChelloHellohello1WOrld
Perform sort and use uniq to ignore case sensitivity
[zhang@localhost ~]$ cat 2.txt | sort | uniq -iabchellohello1WOrld
Perform sort and use uniq to count
[zhang@localhost ~]$ cat 2.txt | sort | uniq -c 2 abc 1 ABC 1 hello 1 Hello 1 hello1 1 WOrld