Linux FAQ 3, sed

Source: Internet
Author: User
Linux:; sed: 1. sedsed stands for StreamEditor, which is widely used as a filter in Shell scripts and makefiles, that is to say, the output of the previous program is introduced into the sed input, and the output is converted into another format through a series of editing commands .... In linux, I learned about sed. 1. sed stands for Stream Editor. it is widely used as a filter in Shell scripts and makefiles, that is, the output of the previous program is introduced into the sed input, and the output is converted into another format by a series of editing commands. Both sed and vi are derived from the early UNIX ed tools. Therefore, many sed commands are the same as the last-line commands of vi. The basic format of sed command line is sed option 'script' file1 file2... sed option-f scriptfile file1 file2... the files processed by sed can be obtained through standard input redirection, or when the command line parameters are passed in, the command line parameters can be imported into multiple files at a time, and sed will process the files in sequence. The sed editing command can be passed in directly as a command line parameter or written as a script file and then specified with the-f parameter. the editing command format is/pattern/action, where pattern is a regular expression, action is an edit operation. The sed program reads the files to be processed in one row. if a line matches pattern, the corresponding action is executed. if a command does not have pattern but only action, the action acts on each row of the files to be processed. Common sed commands/pattern/p print lines that match pattern/d delete lines that match pattern/s/pattern1/pattern2/find lines that match pattern, replace the first string that matches pattern1 with pattern2/pattern/s/pattern1/pattern2/g to find the line that matches pattern, replace all strings matching pattern1 in this row with pattern2. Note that sed outputs the content of the file to be processed together with the processing result to the standard output, therefore, the p command indicates that in addition to printing out the file content, the line matching pattern is printed again. For example, if the content of testfile in a file is 123abc456 and the row containing abc $ sed '/abc/p' testfile123abcabc456 is printed, add the-n option to output only the processing result, this usage is equivalent to the grep command $ sed-n'/abc/p' testfileabc. the-n parameter is not required when using the d command, for example, delete the $ sed '/abc/d' testfile123456 line containing abc. Note that the sed command does not modify the original file. The delete command only indicates that some lines do not print the output, rather than deleting the lines from the original file. When using the search replacement command, you can copy the string matching pattern1 to pattern2, for example: $ sed's/bc: $ sed's/\ ([0-9] \) \ ([0-9] \)/-\ 1 -~ \ 2 ~ /'Testfile-1 -~ 2 ~ 3abc-4 -~ 5 ~ In 6pattern2, \ 1 indicates the content that matches the first () bracket of pattern1, and \ 2 indicates the content that matches the second () bracket of pattern1. Sed uses the Basic regular expression specification by default. If the-r option is specified, the Extended specification is used. () parentheses do not need to be escaped. If the content of testfile is Hello WorldWelcome to the world of regexp!Remove all HTML tags so that the output result is Hello WorldWelcome to the world of regexp! How can this problem be solved? If the following command $ sed's/<. *> // g' testfile is used to return two blank lines, all characters are filtered out. This is because the number qualifier in the regular expression will match the string as long as possible. this is called Greedy (Greedy). For example, when sed is processing the first row, <. *> The match is notOrInstead Hello WorldThis is an entire line, because this line starts with <, contains several arbitrary characters in the middle, and ends with>.
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.