Linux sed commands

Source: Internet
Author: User
  1. 1. Introduction to SED
  2. Sed is an online editor that processes a row of content at a time. During processing, the currently processed rows are stored in the temporary buffer, called the pattern space. Then, the SED command is used to process the content in the buffer, send the buffer content to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output. Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, and write conversion programs. The following describes the GNU sed 3.02 version.
  3. 2. Addressing
  4. The address can be used to locate the row you want to edit. The address is composed of numbers, the two rows separated by commas indicate the range of rows starting and ending with the two rows (including the rows indicated by the number of rows ). For example, 1 and 3 indicate 1, 2, 3 rows, and the dollar sign ($) indicates the last row. The range can be determined by data, regular expressions, or a combination of the two.
  5. 3. Sed command
  6. The SED command can be called in two forms:
  7. *
  8. Sed [Options] 'command' file (s)
  9. *
  10. Sed [Options]-F scriptfile file (s)
  11. A/
  12. Add a line of text behind the current line.
  13. B lable
  14. Branch to the marked place in the script. If the branch does not exist, branch to the end of the script.
  15. C/
  16. Use new text to change the text of this line.
  17. D
  18. Deletes a row from the position of the template block (pattern space.
  19. D
  20. Delete the first line of the template block.
  21. I/
  22. Insert text on the current row.
  23. H
  24. Copy the content of the template block to the buffer in the memory.
  25. H
  26. Append the content of the template block to the buffer in the memory.
  27. G
  28. Obtain the content of the memory buffer and replace the text in the current template block.
  29. G
  30. Obtain the content of the memory buffer and append it to the end of the current template block text.
  31. L
  32. List of characters that cannot be printed.
  33. N
  34. Read the next input line and use the next command to process the new line instead of the first command.
  35. N
  36. Append the next input row to the end of the template block and embed a new line between them to change the number of the current row.
  37. P
  38. Print the row of the template block.
  39. P (uppercase)
  40. Print the first line of the template block.
  41. Q
  42. Exit sed.
  43. R File
  44. Read rows from the file.
  45. T label
  46. IfBranch, starting from the last line. Once the condition is met or the T, T command will cause the branch to the command with a label or to the end of the script.
  47. T label
  48. An error branch starts from the last line. If an error occurs or the T or t command, the Branch is routed to the command with a label or to the end of the script.
  49. W File
  50. Write and append the template block to the end of the file.
  51. W File
  52. Write and append the first row of the template block to the end of the file.
  53. !
  54. Indicates that the subsequent commands will work on all unselected lines.
  55. S/RE/string
  56. Use string to replace the regular expression re.
  57. =
  58. Print the number of the current row.
  59. #
  60. Extend the annotation to the next line break.
  61. Replace tags
  62. *
  63. G indicates that all rows are replaced.
  64. *
  65. P indicates printing rows.
  66. *
  67. W indicates writing rows into a file.
  68. *
  69. X indicates the text in the SWAp template block and the text in the buffer.
  70. *
  71. Y indicates translating a character into another character (but not used in regular expressions)
  72. 4. Options
  73. -E command, -- Expression = command
  74. Multiple edits are allowed.
  75. -H, -- Help
  76. Print help and display the address of the Bug list.
  77. -N, -- quiet, -- silent
  78. Cancel default output.
  79. -F, -- filer = script-File
  80. The name of the bootstrap sed script file.
  81. -V, -- version
  82. Print the version and copyright information.
  83. 5. metacharacters ^
  84. The start of the anchor row is as follows:/^ SED/matches all rows starting with sed.
  85. $
  86. The end of the anchor row is as follows:/SED $/matches all rows ending with sed.
  87. .
  88. Match a non-linefeed character, for example,/S. D/match s, followed by any character, and then D.
  89. *
  90. Match zero or multiple characters, such as:/* SED/match all templates with one or more spaces followed by sed rows.
  91. []
  92. Matches a character in a specified range, such as/[ss] ED/matches SED and sed.
  93. [^]
  94. Match a character that is not within the specified range, such as:/[^ A-RT-Z] ED/match a line that is followed by a letter that does not contain a A-R and a T-Z.
  95. /(../)
  96. Save matched characters, such as S // (Love/) able // 1RS, and loveable is replaced with lovers.
  97. &
  98. Save the search characters to replace other characters, such as S/love/** & **/. The love character is ** love **.
  99. /<
  100. Anchor specifies the start of a word, such as: // <love/match a line that contains a word starting with love.
  101. />
  102. Anchor specifies the end of a word, for example,/love/>/matches a row that contains a word ending with love.
  103. X/{M /}
  104. Repeat characters X and M, for example,/0/{5/}/to match rows containing 5 o.
  105. X/{M ,/}
  106. Repeated characters X, at least m times, such as:/O/{5,/}/matched rows with at least 5 o.
  107. X/{M, N /}
  108. Repeated characters X, at least m times, no more than N times, such as:/O/{5, 10/}/rows matching 5-10 o.
  109. 6. Instance
  110. Delete: D command
  111. *
  112. $ Sed '2d 'example ----- Delete the second line of the example file.
  113. *
  114. $ Sed '2, $ d' example ----- delete all rows from the second row to the end of the example file.
  115. *
  116. $ Sed '$ d' example ----- Delete the last row of the example file.
  117. *
  118. $ Sed '/test/'d example ----- delete all rows containing test in the example file.
  119. Replace: s command
  120. *
  121. $ SED's/test/mytest/G' example ----- replace test with mytest in the entire row. If no G tag exists, only the first matched test in each row is replaced with mytest.
  122. *
  123. $ Sed-n's/^ test/mytest/P 'example ----- (-N) option and the P Flag are used together to print only the replaced rows. That is to say, if the test at the beginning of a row is replaced with mytest, print it.
  124. *
  125. $ SED's/^ 192.168.0.1/& localhost/'example ----- & symbol indicates replacing the part found in the string. All rows starting with 192.168.0.1 are replaced with their own localhost and changed to 192.168.0.1localhost.
  126. *
  127. $ Sed-n's // (Love/) able // 1RS/P' example ----- love is marked as 1, and all loveable will be replaced with lovers, the replaced line is printed out.
  128. *
  129. $ SED's #10 #100 # g'example ----- whatever the character, followed by the S command is considered as a new separator. Therefore, "#" is a separator here, replaces the default "/" separator. Replace all 10 with 100.
  130. Range of selected rows: comma
  131. *
  132. $ Sed-n'/test/,/check/P' example ----- all rows within the range specified by the template test and check are printed.
  133. *
  134. $ Sed-n' 5,/^ test/P' example ----- print all rows starting from the fifth line to the first line that contains the beginning of test.
  135. *
  136. $ Sed '/test/,/check/S/$/SED test/' example ----- for the rows between the template test and west, the end of each row is replaced by the string sed test.
  137. Multi-Point Editing: e command
  138. *
  139. $ Sed-e '1, 5d '-E's/test/check/'example ----- (-e) options allow multiple commands to be executed in the same line. As shown in the example, the First Command deletes lines 1 to 5, and the second command replaces test with check. The command execution order has an impact on the result. If both commands are replacement commands, the first replacement command will affect the result of the second replacement command.
  140. *
  141. $ Sed -- Expression ='s/test/check/'-- Expression ='/love/d' example ----- a better command than-E is -- expression. It can assign values to SED expressions.
  142. Read from file: R command
  143. *
  144. $ Sed '/test/R file' example ----- the content in file is read and displayed after the row matching test. If multiple rows match, the file content is displayed under all matched rows.
  145. Write File: W command
  146. *
  147. $ Sed-n'/test/W file' example ----- all rows containing test in example are written to file.
  148. APPEND Command: command
  149. *
  150. $ Sed '/^ test/A // ---> This Is A example 'example <----- 'this is a example' is appended to the row starting with test, sed requires that command a be followed by a backslash.
  151. Insert: I command
  152. $ Sed '/test/I //
  153. NewLine
  154. ------------------------- 'Example
  155. If test is matched, the text following the backslash is inserted before the matching row.
  156. Next: N command
  157. *
  158. $ Sed '/test/{n; S/AA/BB/;}' example ----- if test is matched, move it to the next row of the matched row and replace AA of this row, change to BB, print the row, and continue.
  159. Deformation: y command
  160. *
  161. $ Sed '1, 10y/ABCDE/'example ----- converts all ABCDE in line 1-10 to uppercase. Note that this command is not applicable to the regular expression metacharacters.
  162. Exit: Q command
  163. *
  164. $ Sed '10q' example ----- exit sed after printing the 10th rows.
  165. Maintain and obtain: H and G commands
  166. *
  167. $ Sed-e '/test/H'-e' $ g example ----- when sed processes files, each row is saved in a temporary buffer called a mode space, all processed rows are printed on the screen unless the row is deleted or the output is canceled. The mode space is cleared, and a new row is saved for processing. In this example, the row Matching Test is found and saved to the mode space. The H Command copies the row and saves it to a special buffer zone called the guaranteed cache. The second statement means that when the last line is reached, the G command extracts the row that maintains the buffer and places it back in the mode space, and append it to the end of the row that already exists in the mode space. In this example, It is appended to the last row. Simply put, any row containing test is copied and appended to the end of the file.
  168. Persistence and interchange: H and X commands
  169. *
  170. $ Sed-e '/test/H'-e'/check/X' example ----- swap mode space and keep the buffer content. That is, to swap the rows containing test and check.
  171. 7. Script
  172. Sed script is a list of SED commands. When SED is started, the file name of the script is guided by the-F option. Sed is very picky about the commands entered in the script. There cannot be any blank or text at the end of the command. If there are multiple commands in one line, separate them with semicolons. Comments rows starting with # And cannot span rows.
1. Introduction to SED is an online editor that processes a row of content at a time. During processing, the currently processed rows are stored in the temporary buffer, called the pattern space. Then, the SED command is used to process the content in the buffer, send the buffer content to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output. Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, and write conversion programs. The following describes the GNU sed 3.02 version. 2. the address can be used to locate the row you want to edit. The address is composed of numbers, the two rows separated by commas indicate the range of rows starting and ending with the two rows (including the rows indicated by the number of rows ). For example, 1 and 3 indicate 1, 2, 3 rows, and the dollar sign ($) indicates the last row. The range can be determined by data, regular expressions, or a combination of the two. 3. the SED command calls the SED command in two forms: * sed [Options] 'command' file (s) * sed [Options]-F scriptfile file (s) a/Add a line of text behind the current line. B lable branch to the marked place in the script. If the branch does not exist, it is routed to the end of the script. C/use new text to change the text of this line. D. Delete the row from the position of the template block (pattern space. D. Delete the first line of the template block. I/Insert text on the current row. H. Copy the content of the template block to the buffer in the memory. H append the content of the template block to the buffer g in the memory to obtain the content of the memory buffer and replace the text in the current template block. G to obtain the content of the memory buffer and append it to the end of the block Text of the current template. L The list cannot print the character list. N. Read the next input line and use the next command to process the new line instead of the first command. N append the next input row to the template block and embed a new line between them to change the number of the current row. P prints the row of the template block. P (uppercase) prints the first line of the template block. Q: Exit sed. R file reads rows from the file. The T labelif branch starts from the last line. Once the condition is met or the T and T commands are executed, the Branch is routed to the command with a label or to the end of the script. T label indicates the branch with an error. Starting from the last line, once an error occurs or the T and T commands are executed, the Branch is routed to the command with a label or to the end of the script. W file write and append the template block to the end of the file. W file write and append the first line of the template block to the end of the file .! Indicates that the subsequent commands will work on all unselected lines. S/RE/string Replace the regular expression re with string. = Print the current row number. # Extend the annotation to the next line break. The replacement mark * G indicates full replacement in the row. * P indicates printing rows. * W indicates writing rows into a file. * X indicates the text in the SWAp template block and the text in the buffer. * Y indicates to translate a character into another character (but not for a regular expression) 4. Option-e command, -- Expression = command allows multiple edits. -H, -- help: print the help and display the address of the Bug list. -N, -- quiet, -- silent cancel the default output. -F, -- filer = script-file guide sed script file name. -V, -- version: print the version and copyright information. 5. The starting point of the metadatabase Character Set ^ anchor is as follows:/^ SED/matches all rows starting with sed. $ The End Of The Anchor row is as follows:/SED $/matches all rows ending with sed.. Match a non-linefeed character such as:/S. D/match s followed by any character, then D. * Match zero or multiple characters, such as:/* SED/match all templates with one or more spaces followed by sed rows. [] Matches a character in a specified range, such as/[ss] ED/matches SED and sed. [^] Match a character that is not within the specified range, for example:/[^ A-RT-Z] ED/match a line that is followed by a letter that does not contain a A-R and a T-Z. /(../) Saves matching characters, such as S // (Love/) able // 1RS, and loveable is replaced with lovers. & Save the search characters to replace other characters, such as S/love/** & **/. The love character is ** love **. /<Specifies the start of a word, such as: // <love/matches a row that contains a word starting with love. /> Pin the end of a word, such as/love/>/match the row containing the word ending with love. X/{M/} repeated characters X, m times, such as:/0/{5/}/matched rows containing 5 o. X/{M,/} repeat character X, at least m times, such as:/O/{5, //}/matched rows with at least 5 o. X/{M, N/} repeats the character X, at least m times, no more than N times, for example,/O/{5, 10/}/matches rows of 5--10 O. 6. delete an instance: D command * $ sed '2d 'example ----- Delete the second line of the example file. * $ Sed '2, $ d' example ----- delete all rows from the second row to the end of the example file. * $ Sed '$ d' example ----- Delete the last row of the example file. * $ Sed '/test/'d example ----- delete all rows containing test in the example file. Replace: s command * $ SED's/test/mytest/G' example ----- replace test with mytest in the entire line. If no G tag exists, only the first matched test in each row is replaced with mytest. * $ Sed-n's/^ test/mytest/P' example ----- (-N) option and the P Flag are used together to print only the replaced rows. That is to say, if the test at the beginning of a row is replaced with mytest, print it. * $ SED's/^ 192.168.0.1/& localhost/'example ----- & symbol represents the part found in the replacement string. All rows starting with 192.168.0.1 are replaced with their own localhost and changed to 192.168.0.1localhost. * $ Sed-n's // (Love/) able // 1RS/P' example ----- love is marked as 1, and all loveable is replaced with lovers, the replaced line is printed out. * $ SED's #10 #100 # g'example ----- whatever the character, followed by the S command is considered as a new separator. Therefore, "#" is a separator here, replaces the default "/" separator. Replace all 10 with 100. The range of selected rows: comma * $ sed-n'/test/,/check/P' example ----- all rows within the range specified by the template test and check are printed. * $ Sed-n'5,/^ test/P' example ----- print all rows starting from the fifth line to the first line containing the start of test. * $ Sed '/test/,/check/S/$/SED test/'example ----- for the rows between the template test and west, the end of each row is replaced by the string sed test. Multi-Point Editing: e command * $ sed-e '1, 5d '-E's/test/check/'example ----- (-E) option allows multiple commands to be executed in the same line. As shown in the example, the First Command deletes lines 1 to 5, and the second command replaces test with check. The command execution order has an impact on the result. If both commands are replacement commands, the first replacement command will affect the result of the second replacement command. * $ Sed -- Expression ='s/test/check/'-- Expression ='/love/d' example ----- a better command than-E is -- expression. It can assign values to SED expressions. Read from a file: the content in the R command * $ sed '/test/R file 'example ----- file is read and displayed after the line Matching Test. If multiple lines match, the file content is displayed under all matched rows. Write File: W command * $ sed-n'/test/W file' example ----- in example, all rows containing test are written into file. APPEND command: A command * $ sed '/^ test/A // ---> This Is A example 'example <----- 'this is a example' is appended to the end of the row starting with test, sed requires that command a be followed by a backslash. Insert: I command $ sed '/test/I // New Line -------------------------' example if test is matched, insert the text following the backslash to the front of the matching line. Next: N command * $ sed '/test/{n; S/AA/BB/;}' example ----- if test is matched, move it to the next line of the matching line, replace the AA of this row with BB, print the row, and continue. Deformation: The y command * $ sed '1, 10y/ABCDE/'example ----- converts all ABCDE in line 1-10 to uppercase. Note that this command cannot be used for regular expression metacharacters. Exit: Q command * $ sed '10q' example ----- exit sed after printing the 10th rows. Keep and get: H command and G command * $ sed-e '/test/H'-e' $ g example ----- when sed processes files, each row is saved in a temporary buffer called the mode space. Unless the row is deleted or the output is canceled, all processed rows are printed on the screen. The mode space is cleared, and a new row is saved for processing. In this example, the row Matching Test is found and saved to the mode space. The H Command copies the row and saves it to a special buffer zone called the guaranteed cache. The second statement means that when the last line is reached, the G command extracts the row that maintains the buffer and places it back in the mode space, and append it to the end of the row that already exists in the mode space. In this example, It is appended to the last row. Simply put, any row containing test is copied and appended to the end of the file. Keep and swap: H command and X command * $ sed-e '/test/H'-e'/check/X' example ----- swap mode space and keep the content of the buffer. That is, to swap the rows containing test and check. 7. The SED script is a sed command list. When SED is started, the file name of the script is guided by the-F option. Sed is very picky about the commands entered in the script. There cannot be any blank or text at the end of the command. If there are multiple commands in one line, separate them with semicolons. Comments rows starting with # And cannot span rows.

 

Sed can greatly improve our work efficiency. Below we write this line, and many files are replaced, which is really convenient.

Sed's/localhost/127.0.0.1/G' mysql_virtual _ *. cf

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.