Sed command learning Notes

Source: Internet
Author: User
Sed command learning Notes 1. sed introduction sed is a non-interactive editor. It does not modify the file unless you use shell redirection to save the results. By default, all output rows are printed to the screen. Sed editor processes files (or inputs) row by row and sends the results to the screen. Detailed process... sed command learning Notes 1. sed introduction sed is a non-interactive editor. It does not modify the file unless you use shell redirection to save the results. By default, all output rows are printed to the screen. Sed editor processes files (or inputs) row by row and sends the results to the screen. The specific process is as follows: first, sed stores the currently being processed rows in a temporary cache (also known as the mode space), and then processes the rows in the temporary buffer, then, send the row to the screen. Sed deletes a row from the temporary buffer after processing, and then reads the next row for processing and display. After processing the last line of the input file, sed stops running. Sed stores each row in a temporary buffer and edits the copy. Therefore, the original file is not modified. 2. the address is used to determine which rows to edit. The address format can be numbers, regular expressions, or a combination of the two. If the address is a number, this indicates the row number. if it is $, it indicates the last row. for example: 01 # print the third line 02sed-n '3' example03 04 # view the 10 to 30 lines of the file 05sed-n' 10, 30p 'example06 07 # from row 10th to end 08sed-n' 10, $'example09 10 # print the line from containing "first" to line 11sed-n containing "python"/first /, /python/p'example12 13 # delete rows containing "linux" to lines 14sed '/linux /, 10d 'example 15 16 # delete all the lines containing test in the example File: 17sed '/test/'d example 3 and the sed command tells sed how to process the input lines specified by the address, if no address is specified, all input rows are processed. 3.1 sed command function a \ add one or more lines after the current line. For multiple rows, except the last line, the end of each line must replace the text in the current line with the new text after the symbol "\" for the continued Row c. For multiple rows, except the last row, you need to insert text before the current row by "\" continuation line I \ at the end of each line. Multiple rows except the last row, at the end of each row, you need to use "\" to continue Row d to delete row h to copy the content in the mode space to the temporary buffer H to append the content in the mode space to the temporary buffer g to copy the content in the temporary buffer mode space, overwrite the original content G and append the content of the temporary buffer to the mode space. append the content to the end of the original content l list non-printed characters p print row n and read it into the next input line, and starts to process the next command instead of the first command. q ends or exits sed r and reads the input line from the file! Apply command s to all rows other than the selected row replace one string with another g globally replace w write the selected row into the file x swap the contents of the temporary buffer and the mode space y replace the characters replace with another character (the y command cannot be used for the regular expression) 3.2 sed option function-e multiple edits, that is, when multiple sed commands are applied to the input line, use-n to cancel the default output-f to specify the sed script file name 4 Example 01 # Replace a in the tt file with mytest02sed's/ /mytest/g'tt03 04 # Replace a in the tt file with you05sed's/a/you/g' tt06 # if there is no g tag, then, only the first matching test in each row is replaced with mytest. 07 08 # replace the first two rows with 09sed '1, 2 s/a/you/g' tt10 11 # option-e for multiple edits. 12sed-e '1, 10d '-e's/My/Your/g' tt13 # delete row 1-10 in the first record. The second edit replaces all My that appear with Your. 14 # because these two commands are edited line by line (both commands are executed on the current line of the mode space), the order of the commands will affect the result. 15 16 # read command, use this command to add specific file content to the specific line of another file and then 17sed '/a/r tt1 'tt18 # if the row in the tt file contains, add the content in the tt1 file to the back 19 20 # add the content to the tt1 file 21sed '/a/w tt1 'tt22 23 # read the tt file, replace "v" in the next line containing "a" with mytest24sed '/a/{n; s/v/mytest /;} 'tt25 26 # When you read that the row in the tt file contains a, add "you" 27sed's/a/& you/g' tt 28 29 30sed '1, 20y/hrwang12/HRWANG ^ $/'31 # Convert all lowercase hrwang values from 1 to 20 rows to uppercase, convert 1 to ^, and convert 2 to $. 32 # The metacharacter of the regular expression does not work for the y command. A diagonal line can be replaced with other characters.
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.