Linux Sed Command: A Complete Guide

Java code  Bookmark code
  1. 1. Sed Introduction  
  2. sed  is an online editor that processes one line of content at a time. During processing, it stores the currently processed line in a temporary buffer called the “pattern space,” then uses sed commands to process the content in the buffer. After processing, it sends the buffer content to the screen. It then proceeds to the next line, repeating this process until the end of the file. The file content itself is not changed unless you use redirection to store the output. Sed is primarily used for automatically editing one or more files, simplifying repetitive operations on files, and writing conversion programs. The following introduces the GNU version of Sed 3.02.  
  3. 2. Addressing  
  4. You can use addressing to locate the lines you wish to edit. The address is composed of numbers; two line numbers separated by a comma represent the range of lines starting and ending with those two lines (inclusive). For example, 1,3 means lines 1, 2, and 3. The dollar sign ($) represents the last line. The range can be determined by numbers, regular expressions, or a combination of both.  
  5.   
  6. 3. Sed Commands  
  7. There are two forms for calling sed commands:  
  8. *  
  9. sed [options] 'command' file(s)  
  10. *  
  11. sed [options] -f scriptfile file(s)  
  12. a/  
  13. Appends a line of text after the current line.  
  14. b label  
  15. Branches to a marked position in the script; if the branch label does not exist, branches to the end of the script.  
  16. c/  
  17. Changes the text of the current line with new text.  
  18. d  
  19. Deletes lines from the pattern space position.  
  20. D  
  21. Deletes the first line of the pattern space.  
  22. i/  
  23. Inserts text above the current line.  
  24. h  
  25. Copies the contents of the pattern space to the hold buffer in memory.  
  26. H  
  27. Appends the contents of the pattern space to the hold buffer in memory.  
  28. g  
  29. Gets the contents of the hold buffer and replaces the text in the current pattern space.  
  30. G  
  31. Gets the contents of the hold buffer and appends it after the text in the current pattern space.  
  32. l  
  33. Lists non-printable characters.  
  34. n  
  35. Reads the next input line and processes the new line with the next command rather than the first command.  
  36. N  
  37. Appends the next input line to the pattern space, embedding a newline between them, and changes the current line number.  
  38. p  
  39. Prints the lines of the pattern space.  
  40. P (uppercase)  
  41. Prints the first line of the pattern space.  
  42. q  
  43. Exits Sed.  
  44. r file  
  45. Reads lines from file.  
  46. t label  
  47. if branch; starting from the last line, once the condition is met, or with a T or t command, it will cause a branch to the command with the label, or to the end of the script.  
  48. T label  
  49. Error branch; starting from the last line, once an error occurs, or with a T or t command, it will cause a branch to the command with the label, or to the end of the script.  
  50. w file  
  51. Writes and appends the pattern space to the end of file.  
  52. W file  
  53. Writes and appends the first line of the pattern space to the end of file.  
  54. !  
  55. Indicates that the following command applies to all lines that were not selected.  
  56. s/re/string  
  57. Replaces the regular expression re with string.  
  58. =  
  59. Prints the current line number.  
  60. #  
  61. Extends the comment until the next newline character.  
  62. The following are substitution flags  
  63. *  
  64. g means global replacement within the line.  
  65. *  
  66. p means print the line.  
  67. *  
  68. w means write the line to a file.  
  69. *  
  70. x means exchange the text in the pattern space with the text in the hold buffer.  
  71. *  
  72. y means translate one character into another (but not used for regular expressions).  
  73.   
  74. 4. Options  
  75. -e command, –expression=command  
  76. Allows multiple edits.  
  77. -h, –help  
  78. Prints help and displays the address for bug lists.  
  79. -n, –quiet, –silent  
  80.   
  81. Cancels the default output.  
  82. -f, –filer=script-file  
  83. Directs sed to use a script file.  
  84. -V, –version  
  85. Prints version and copyright information.  
  86.   
  87. 5. Meta-character Set ^  
  88. Anchors the beginning of a line. E.g., /^sed/ matches all lines starting with sed.   
  89. $  
  90. Anchors the end of a line. E.g., /sed$/ matches all lines ending with sed.   
  91. .  
  92. Matches any single character except newline. E.g., /s.d/ matches s followed by any character, then d.   
  93. *  
  94. Matches zero or more characters. E.g., /*sed/ matches all lines where the pattern is one or more spaces followed immediately by sed.  
  95. [] 
  96. Matches any character within a specified range. E.g., /[Ss]ed/ matches both sed and Sed.  
  97. [^] 
  98. Matches any character not within a specified range. E.g., /[^A-RT-Z]ed/ matches lines starting with a letter not in the range A-R and T-Z, followed by ed.  
  99. /(../) 
  100. Save matched characters, e.g., s//(love/)able//1rs, loveable becomes lovers.  
  101. & 
  102. Save the search string to replace other characters, e.g., s/love/**&**/, love becomes **love**.   
  103. /<  
  104. Anchor at the start of a word, e.g.,://<love/ matches lines containing words starting with love.   
  105. />  
  106. Anchor at the end of a word, e.g., /love/>/ matches lines containing words ending with love.   
  107. x/{m/}  
  108. Repeat character x, m times, e.g., /0/{5/}/ matches lines containing 5 o’s.   
  109. x/{m,/}  
  110. Repeat character x, at least m times, e.g., /o/{5,/}/ matches lines with at least 5 o’s.   
  111. x/{m,n/}  
  112. Repeat character x, at least m times, and at most n times, e.g., /o/{5,10/}/ matches lines with 510 o’s.  
  113. 6. Examples  
  114. Delete: d command  
  115. *  
  116. $ sed '2d' example—–Delete the second line of the example file.  
  117. *  
  118. $ sed '2,$d' example—–Delete all lines from the second line to the end of the example file.  
  119. *  
  120. $ sed '$d' example—–Delete the last line of the example file.  
  121. *  
  122. $ sed '/test/'d example—–Delete all lines containing ‘test’ in the example file.  
  123. Substitute: s command  
  124. *  
  125. $ sed 's/test/mytest/g' example—–Replace ‘test’ with ‘mytest’ on the entire line. Without the g flag, only the first matched ‘test’ on each line is replaced with ‘mytest’.  
  126. *  
  127. $ sed -n 's/^test/mytest/p' example—–The (-n) option combined with the p flag means to print only lines where a substitution occurred. That is, if ‘test’ at the beginning of a line is replaced with ‘mytest’, print that line.  
  128. *  
  129. $ sed 's/^192.168.0.1/&localhost/' example—–The & symbol represents the part of the replacement string that was found. All lines starting with 192.168.0.1 will be replaced by themselves plus localhost, becoming 192.168.0.1localhost.  
  130. *  
  131. $ sed -n 's//(love/)able//1rs/p' example—–love is tagged as 1, all occurrences of loveable will be replaced with lovers, and the replaced lines will be printed.  
  132. *  
  133. $ sed 's#10#100#g' example—–Regardless of the character, whatever immediately follows the s command is considered the new delimiter. So, “#” is the delimiter here, replacing the default “/” delimiter. This means replace all 10 with 100.  
  134. Selecting a range of lines: Comma  
  135. *  
  136. $ sed -n '/test/,/check/p' example—–All lines within the range determined by the patterns test and check are printed.  
  137. *  
  138. $ sed -n '5,/^test/p' example—–Print all lines from the fifth line up to the first line that starts with test.  
  139. *  
  140. $ sed '/test/,/check/s/$/sed test/' example—–For lines between the patterns test and check, replace the end of each line with the string sed test.  
  141. Multi-point editing: e command  
  142. *  
  143. $ sed -e '1,5d' -e 's/test/check/' example—–The (-e) option allows multiple commands to be executed on 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 order of command execution affects the results. If both commands are substitution commands, the first substitution will affect the result of the second.  
  144. *  
  145. $ sed –expression='s/test/check/' –expression='/love/d' example—–A better command than -e is –expression. It allows assigning values to sed expressions.  
  146. Read from file: r command  
  147. *  
  148. $ sed '/test/r file' example—–The contents of file are read in and displayed after lines matching test. If there are multiple matching lines, the content of file will be displayed below all matching lines.  
  149. Write to file: w command  
  150. *  
  151. $ sed -n '/test/w file' example—–All lines in example containing test are written to file.  
  152. Append command: a command  
  153. *  
  154. $ sed '/^test/a//—>this is a example' example<—–'this is a example' is appended after lines starting with test. The sed command requires a backslash after the a command.  
  155. Insert: i command  
  156. $ sed '/test/i//  
  157. new line  
  158. ————————-' example  
  159. If test is matched, the text after the backslash is inserted before the matching line.  
  160. Next: the n command  
  161. *  
  162. $ sed '/test/{ n; s/aa/bb/; }' example—–If test is matched, move to the next line of the matching line, replace aa with bb on that line, print the line, and then continue.  
  163. Transform: the y command  
  164. *  
  165. $ sed '1,10y/abcde/ABCDE/' example—–Transform all characters abcde to uppercase within lines 1 to 10. Note that regex metacharacters cannot be used with this command.  
  166. Quit: the q command  
  167. *  
  168. $ sed '10q' example—–After printing the 10th line, quit sed.  
  169. Hold and Get: the h command and G command  
  170. *  
  171. $ sed -e '/test/h' -e '$G example—– When sed processes a file, each line is stored in a temporary buffer called the pattern space. Unless a line is deleted or output is suppressed, all processed lines are printed to the screen. The pattern space is then cleared and a new line is loaded for processing. In this example, after a line matching test is found and placed in the pattern space, the h command copies it to a special buffer called the hold space. The second statement means: when the last line is reached, the G command takes the content of the hold space and appends it back into the pattern space, after the line currently there. In this case, it appends it to the last line. Simply put, any line containing test is copied and appended to the end of the file.  
  172. Hold and Exchange: the h command and x command  
  173. *  
  174. $ sed -e '/test/h' -e '/check/x' example —–Exchange the contents of the pattern space and the hold space. In other words, swap the lines containing test and check.  
  175. 7. Scripts  
  176. A Sed script is a list of sed commands, launched with the -f option followed by the script filename. Sed is very particular about the commands in the script; there must be no trailing whitespace or text at the end of a command. If multiple commands are on one line, they must be separated by semicolons. Lines starting with # are comments and cannot span multiple lines.  

 

sed can truly boost our productivity significantly. For instance, I just wrote this single line, and it replaced content in many files — so convenient.

Java code  Bookmark code
  1. sed 's/localhost/127.0.0.1/g' mysql_virtual_*.cf 

Leave a Comment

Your email address will not be published.