Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Linux pipe command

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail how to use Linux pipeline commands for you, with detailed content, clear steps and proper handling of details. I hope this article "how to use Linux pipeline commands" can help you solve your doubts.

Pipeline is one of the most basic IPC mechanism, which acts between consanguineous processes to complete data transmission. You can create a pipe by calling the pipe system function.

Pipe command

The pipe command uses | as a delimiting symbol, and the pipe command is different from the continuous execution command described above.

Pipe commands can only handle standard output and are ignored for standard error output. Less,more,head,tail... Can accept standard input commands, so they are pipe commands ls,cp,mv will not accept standard input commands, so they are not pipe commands. The pipeline command must be able to accept the data from the previous command as standard input to continue processing. The first pipe command $ls-al / etc | less

The output of ls-al is used as the input of the next command less through the pipeline, which is convenient for browsing.

Img

Processing diagram of pipeline command

Select command: cut.grep**cut:** takes a certain part of the information we want from a line of information.

Cut-d 'delimiter'-f field / / for delimiter ``cut-c character range`` [parameter description] ``- d: followed by a delimiter, usually used with-f``-f: separate information into segments according to-d -f followed by a number indicates the paragraph ``- c: take out the information of the fixed character range in characters 1: the first and sixth fields in the print / etc/passwd file with: delimiter denote the user name and home directory [root@izuf6i29flb2df231kt91hz /] # cat etc/passwd | cut-d':'- f 1prime6rootdislmenxrootbindisplacedrootbinbindaemonGansbinadmldandvaradmlpplespedeband LPD... Chestnut 2: print the first 10 characters of each line in the / etc/passwd file: [root@izuf6i29flb2df231kt91hz /] # cat / etc/passwd | cut-c 1-10 rootlace / etc/passwd | cut-c 1-10 rootlvl-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1

* it is difficult for ps:cut to deal with data connected by multiple spaces. *

* * grep:** analyzes a line of information, and if there is any information we need, take it out.

Grep [- acinv] [--color=auto] 'find string' filename`` [Parameter] ``- a: use binary file to find data as text file``-c: calculate the number of times to find 'find string' ``- I: ignore case difference.`-n: output line number``-v: reverse selection Display lines ``- color=auto: color the found keywords 3: take out the line [root@izuf6i29flb2df231kt91hz /] # cat etc/passwd containing the fanco / etc/passwd file | grep-n-c 'fanco'1 [root@izuf6i29flb2df231kt91hz /] # cat etc/passwd | grep-n' fanco'23:fanco:x:1001:1001::/home/fanco:/bin/bash [root@izuf6i29flb2df231kt91hz /] # cat etc/passwd | Grep-n-v 'fanco'1:root:x:0:0:root:/root:/bin/bash2:bin:x:1:1:bin:/bin:/sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4:adm:x:3:4:adm:/var/adm:/sbin/nologin5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin...img

Add the-color parameter, it seems to have a color without default.

Sort command: sort,wc,uniq

Sort

Sort [- fbMnrtuk] [file or stdin] ``[Parameter]``-f: ignore the case difference. For example, An and an are considered to have the same code ``- b: ignore the first space``-M: sort by month's name, such as JAN, DEC, etc. ``- n: sort by text type by default using "pure number")``-r: reverse sort ``- u: uniq, in the same data Only one line represents the ``- t: separator The default is to use the [tab] key to separate ``- k: sort by that field. Chestnut 4: sort the accounts of / etc/passwd [root@izuf6i29flb2df231kt91hz /] # cat / etc/passwd | sortadm:x:3:4:adm:/var/adm:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologinchrony:x:998:996::/var/lib/chrony:/sbin/nologin... Sort by column 5 of / etc/passwd [root@izuf6i29flb2df231kt91hz /] # cat etc/passwd | sort-t':'- k 3root:x:0:0:root:/root:/bin/bashfanco:x:1001:1001::/home/fanco:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp: X:14:50:FTP User:/var/ftp:/sbin/nologin, the sort is still sorted by text. Switch to numerical sort [root@izuf6i29flb2df231kt91hz /] # cat etc/passwd | sort-t':'- k 3-nroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinuniq

Uniq [- ic] ``[ Parameter] ``- I: ignore case differences``-c: count the accounts of Lizi 5 using last to take out historical login information, sort, and remove [root@izuf6i29flb2df231kt91hz /] # last | cut-d'- f 1 | sort | uniq-c 1 7 reboot 19 root 1 wtmpwc

Wc [- lwm] ``[ Parameter] ``- l: line only``-w: number of words (English words) ``- m: how many characters Chestnut 6 check how many accounts are in etc/passwd [root@izuf6i29flb2df231kt91hz /] # cat / etc/passwd | wc-L23 calculates the number of people recently logged into the system [root@izuf6i29flb2df231kt91hz /] # last | grep [a-zA-Z] | grep-v 'wtmp' | wc-L2 checks the number of lines of a file Words and characters [root@izuf6i29flb2df231kt91hz /] # cat etc/passwd | wc 23 32 997 two-way redirection command: tee

Tee:

A certain piece of information is saved during the processing of the data stream, so that it can be output to both the screen and a file.

Img

Workflow of tee. PNG

Tee [- a] file`` [parameters] ``- a: add data to file in a cumulative way. Chestnut 7 queries recent user logins. And save it to the file [root@izuf6i29flb2df231kt91hz /] # last | tee info | cut-d'- f 1root. [root@izuf6i29flb2df231kt91hz /] # less inforoot pts/0 112.28.181.159 Sun Jul 1 14:28 still logged in root pts/0 112.28.181.159 Sun Jul 1 14:24-14:27 (00:03) root pts/0 112.28.181.159 Sun Jul 1 13:19-14:24 (01:04) root tty1 Sun Jul 1 12:46 still logged in

If the file followed by tee already exists, the content will be overwritten and the-a parameter will be added

Character conversion command: tr,col,join,paste,expand**tr:** is used to delete the text in a message or to replace the text message.

Tr [- ds] set`` [parameter] ``- d: delete the string set1 in the message``-s: replace the duplicate character Chestnut 8 delete all info files generated in the previous step [root@izuf6i29flb2df231kt91hz /] # cat inforoot pts/0 112.28.180.86 Thu May 10 18:01-18:12 (00:11) reboot system boot 3.10.0-693.2 before deletion .2.e Fri May 11 02:00-16:31 (51mm 14cat info 30) after deletion [root@izuf6i29flb2df231kt91hz /] # cat info | tr-d 'root' ps/0 112.28.180.86 Thu May 10 18:01-18:12 (00:11) eb sysem b 3.10.0-693.2.2.e Fi May 11 02:00-16cat info 31 (51mm 14cat info) deletion does not only delete consecutive characters Reboot has also been deleted. The root part removes the ^ M symbol left by the dos file $cat / root/passwd | tr-d'\ r'> / root/ passwd.Linux ^ M can replace col with\ r.

Col [- xb] ``[Parameter]``-x: replace the tab key with the equivalent space bar ``- b: when there is a backslash (/) in the text, only the last character of the backslash is retained. Img Chestnut 9 replace the ^ I in the image above with the space bar [root@izuf6i29flb2df231kt91hz /] # cat info | col-x | cat-A | more root pts/0 112.28.181.159 Sun Jul 1 14:28 still logged in$

Col is often used to dump man page to plain text files

* * join:** mainly talks about a line in which two files have the same data, with the same fields in front of them.

Join [- ti12] file1 file2`` [parameter] ``: join separates the data with a space character by default, and compares the data of the first field, if the two files are the same Then concatenate the two pieces of data into a line ``- I: ignore the case difference``-1: indicate the first file uses which field to analyze``-2: indicate that the second file uses that field to analyze chestnut 10 to integrate / etc/passwd and / etc/shadow-related data into a column [root@izuf6i29flb2df231kt91hz /] # head-3 / etc/passwd / etc/shadow== > / etc/passwd / etc/shadow $6 $RNGEziM7 $2e/EJd3hThS8TMqHSgDIfeDf7dJUG1dbJ0ik1goybGYmLGZL .sHNv1Ltd b4.1HUksxTI0Cs3PJw5g / YirSImKg1:17643:0:99999:7:::bin:*:17110:0:99999:7:::daemon:*:17110:0:99999:7::: [root@izuf6i29flb2df231kt91hz /] # join-t':'/ etc/passwd / etc/shadowroot:x:0:0:root:/root:/bin/bash:$6 $RNGEziM7 $2e/EJd3hThS8TMqHSgDIfeDf7dJUG1dbJ0ik1goybGYmLGZL.sHNv1Ltb4.1HUksxTI0Cs3PJw5g/YirSImKg1:17643:0:99999:7:::bin:x:1:1:bin:/bin:/sbin/ Nologin:*:17110:0:99999:7:::daemon:x:2:2:daemon:/sbin:/sbin/nologin:*:17110:0:99999:7::: compares the fourth field of etc/passwd separated by: with the third field of etc/group If the same Then put the data of his two peers together [root@izuf6i29flb2df231kt91hz /] # join-t':'- 14 / etc/passwd-23 / etc/group0:root:x:0:root:/root:/bin/bash:root:x:1:bin:x:1:bin:/bin:/sbin/nologin:bin:x:2:daemon:x:2:daemon:/sbin:/sbin/nologin:daemon:x:4:adm:x:3:adm:/var/adm: / sbin/nologin:adm:x:join: / etc/passwd:6: is not sorted: sync:x:5:0:sync:/sbin:/bin/sync7:lp:x:4:lp:/var/spool/lpd:/sbin/nologin:lp:x:paste: paste two lines of two files together directly Separated by [tab] key in the middle

Paste [- d] file1 file2`` [parameter] ``- d: can be followed by a delimiter. By default,``-separated by [tab]: if the file part is written as- Express acceptance of standard input data Chestnut 11 [root@izuf6i29flb2df231kt91hz /] # paste info info2 root pts/0 112.28.181.159 Sun Jul 1 14:28 still logged in root pts/0 112.28.181.159 Sun Jul 1 14:28 still logged in root pts/0 112.28.181.159 Sun Jul 1 14:24-14:27 (00:03) root Pts/0 112.28.181.159 Sun Jul 1 14:24-14:27 (00:03) root pts/0 112.28.181.159 Sun Jul 1 13:19-14:24 (01:04) root pts/0 112.28.181.159 Sun Jul 1 13:19-14 root pts/0 24 (01 root pts/0 04) * * expand:** turns the tab key into a space bar

Expand [- t] file [parameter] "- t: followed by a number. Generally, a tab can be replaced by 8 spaces, and you can define how many spaces you want to represent.

Chestnut 12 [root@izuf6i29flb2df231kt91hz /] # cat info | expand-3 info root pts/0 112.28.181.159 Sun Jul 1 14:28 still logged in root pts/0 112.28.181.159 Sun Jul 1 14:24-14:27 (00:03) root pts/0 112.28.181.159 Sun Jul 1 13:19-14:24 (01:04) root tty1 Sun Jul 1 12:46 still logged in cutting command: split

Split: as the name implies, a large file is cut into small files according to file size or number of lines.

Split [- bl] file prefix`` [parameter] ``- b: the size of the file you want to cut can be followed by the size of the file you want to cut, and you can add units, for example, bmeme kprim, etc.``-l: cut by the number of lines ``PREFIX: represents the leading character Chestnut $split-b 300K / etc/passwd divides the ls-al output file into a new file [root@izuf6i29flb2df231kt91hz /] # ls-al / | split-l 10-lsrrot [root@izuf6i29flb2df231kt91hz /] # lsb boot dev home info2 lib64 lsrrotaa lsrrotac mnt opt root sbin sys usrbin c etc info lib lost+found lsrrotab media n proc run srv tmp var [root@izuf6i29flb2df231kt91hz /] # cd / [root@] by 10 lines Izuf6i29flb2df231kt91hz /] # lsb boot dev home info2 lib64 lsrrotaa lsrrotac mnt opt root sbin sys usrbin c etc info lib lost+found lsrrotab media n proc run srv tmp var [root@izuf6i29flb2df231kt91hz /] # wc-l lsrrot* 10 lsrrotaa 10 lsrrotab 9 lsrrotac 29 total This article "how to use Linux pipeline commands" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report