In-depth explanation of redirection and pipelines in linux

Source: Internet
Author: User
In-depth explanation of redirection and pipelines in linux 1. redirect the 11 redirection symbol & gt; redirect the output to a file or device to overwrite the original file & gt ;! Redirect the output to a file or device to forcibly overwrite the original file & gt; redirect the output to a file or device to go deep into linux and review the pipeline, redirect 1.1 redirection symbol> the output is redirected to a file or the device overwrites the original file>! Redirect output to a file or device to forcibly overwrite the original file> redirect output to a file or device to append the original file <input to a program 1.2 standard error redirection symbol 2> standard error output redirects to a file or device overwriting the original file b-shell2> redirects a standard error output to a file or device appending to the original file 2> & 1 redirects a standard error the output is redirected to the standard output comment: 1 may be the standard output> & redirect a standard error output to a file or the device overwrites the original file c-shell | & send a standard error pipeline to another command as the input 1.3 command redirection example in the bash command execution process, there are three output statuses: 1. standard input; code: 0; or stdin; usage: <2. standard output: the code is 1; or stdout; the method used is 1> 3. error output: the code is 2; or stderr; the method used is 2> example: C code [test @ test] # ls-al> list.txt # outputs the displayed result to the list.txt file. if the file exists, it is replaced! [Test @ test] # ls-al> list.txt # add the displayed results to the list.txt file. This file is accumulative and the old data is retained! [Test @ test] # ls-al 1> list.txt 2> list. err # correctly output the displayed data to the list.txt error data to the list. err [test @ test] # ls-al 1> list.txt 2> & 1 # output the displayed data to list.txt, whether correct or wrong! If an error occurs and the correct file is output to the same file, the preceding method is required! Cannot be written in other formats! [Test @ test] # ls-al 1> list.txt 2>/dev/null # The displayed data is discarded if the data is correctly output to the list.txt error! /Dev/null, which can be said to be a black hole device. It is null, that is, it is not saved. 1.4 Why do we need to use command output redirection when the screen output information is important and we need to save it? the program in the background execution, if you do not want him to interfere with the normal output results of the screen, execute the execution results of some system commands (such as files written in/etc/crontab), and save the execution results. execute some commands, we already know the possible error messages, so we want to discard them with "2>/dev/null"; when the error messages and the correct messages need to be output separately. 2. pipeline commands (pipe), as mentioned above, output data appears during bash command execution, so how should we set this group of data to get the desired format after several procedures? This involves pipeline commands (pipe). pipeline commands use '| 』. Example: a simple pipeline command assumes that we want to read the "number" of root logins from The last Command. how can this problem be solved? The steps are as follows: 1. execute last to retrieve all the login data for this month. 2. use grep to extract the root of the above output data (stdout). do not use other methods. 3. you can use the wc command to calculate the number of rows! Since the last output is a line that represents one login, as long as you calculate a few lines, it means several logins. through the above three steps, the last data is gradually filtered, we can get our data! The entire command can be written as follows: [test @ test bin] # last | grep root | wc-l this pipeline command "|" can only process the correct information sent through the previous command, that is, standard output (STDOUT) information does not directly process stdandard errors. 2.1 Basic cut syntax: [root @ test/root] # cut-d "delimiter" [-cf] fields parameter description:-d: the delimiter is followed by the delimiter. the delimiter is a space character.-c: The delimiter is followed by the number of characters.-f: What blocks are followed? Example: C code [root @ test/root] # cat/etc/passwd | cut-d ":"-f 1 # in the file passwd, in each line: used as the separator to list the first block! That's the name! [Root @ test/root] # last | cut-c1-20 # After the last data, each line of 1-20 characters out! Sort syntax: [root @ test/root] # sort [-t separator] [(+ Start) (-end)] [-nru] parameter description:-t separator: use delimiters to separate different intervals. the default delimiter is tab + start-end: sorting from the start interval to the end interval-n: sort by "numbers only" (otherwise it will be sorted by text type)-r: reverse sort-u: the same row that appears, only list once! Example: C code [root @ test/root] # cat/etc/passwd | sort # sort the listed personal accounts! [Root @ test/root] # cat/etc/passwd | sort-t: + 2n # sort the user IDs in the personal account (separated, the third is ID, but the first code is 0.) [root @ test/root] # cat/etc/passwd | sort-t: + 2nr # reverse sorting ?? Wc syntax: [root @ test/root] # wc [-lmw] parameter description:-l: how many rows-m: How many characters-w: How many word example: C code [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 in this file !? The uniq command is used to delete duplicate rows and only display one. Syntax: [root @ test/root] # uniq example: C code [root @ test/root] # last | cut-d ""-f1 | sort | uniq tee command redirects data to a file and displays the data on the screen syntax: [root @ test/root] # last | tee last. list | cut-d ""-f1 example: C code [root @ test/root] # last | tee last. list | cut-d ""-f1 tr syntax: [root @ test/root] # tr [-ds] SET1 parameter description:-d: delete the SET1 string-s: replace repeated characters! Example: C code [root @ test/root] # last | tr' [a-z] ''[A-Z] '# <= change lowercase to uppercase [root @ test/root] # cat/etc/passwd | tr-d: # <==: This symbol is missing in/etc/passwd! [Root @ test/root] # cat/home/test/dostxt | tr-d' \ r'> dostxt-noM split syntax: [root @ test/root] # split [-bl] parameter description of the leading character in the output file of the input file:-B: split by file size-l: example by number of lines: [root @ test/root] # split-l 5/etc/passwd test <= will generate testaa, testab, testac... and so on! If you want to split the file, use-B size to limit the size of the split file. if it is the number of lines, use-l line to separate it! Pipeline commands are very important in bash's continuous processing program! In addition, log file analysis is also an important part. You can use the standard input parameter "-" for more careful control of the standard input for pipeline delivery to a command. example of a cat command: C code sort mylist | more sort mylist | cat-n | lpr pwd | cat-mylist | lpr
Related Article

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.