Function Description: Uses scripts to process text files. Sed can process and edit text files according to script instructions.
Introduction: 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 patternspace. 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.
Two forms:
1. sed [options] '{command}' file (s)
2. sed [options]-f scriptfile file (s)
Method 1: sed-I's/replaced content/content to be replaced/'file Method 2: sed's/replaced content/content to be replaced with/G' file> file. outmv file. out file
Parameter: a \ adds a line of text to the end of the current row. 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. T label if branch, starting from the last line, once the condition is met or T, t command, will lead to branch to the command with the 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 translating a character into another character (but not used in regular expressions)
Option:-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.
Metacharacters: ^ The first line locator,/^ my/match all rows starting with my $ the last line locator,/my $/match all rows ending with my. match a single character except line break,/m .. y/match with the letter m, followed by two arbitrary characters, followed by the letter y line * match with zero or multiple leading characters,/my */match with the letter m, the line [] followed by zero or multiple y letters matches any character in the specified character group, /[Mm] y/match the row that contains My or my [^] matches any character that is not in the specified character group,/[^ Mm] y/matches y, but the character before y is not M or m line \(.. \) Save the matched characters, the pattern between 1, 20 s/\ (you \) self/\ 1r/marked metacharacters, and save it as tag 1, you can use \ 1 to reference it later. A maximum of nine tags can be defined, starting from the left and the first tag on the left. In this example, 1st to 20th rows are processed, and you are saved as tag 1. If you find youself, replace it with your. & Save the search string to reference it in the replacement string. s/my/** & **/Symbol & indicates the search string. My will be replaced with the ** my ** \ <first separator,/\ <my/match the line that contains the word starting with my \> suffix, /my \>/match rows that contain words ending with my
Delete: sed '2d 'example ----- Delete the second row of the example file. Sed '2, $ d' example ----- delete all rows from the second row to the end of the example file. Sed-e '1, 3d 'example ----- (Delete the first to third rows) sed' $ d' example ----- Delete the last row of the example file. Sed '/test/'d example ----- delete all rows containing test in the example file. Sed-e '/#/d' example ----- (delete a row containing) delete the first space sed's/^ [] * // G' filename sed's/^ [[: space:] * // G' filename
Replace: 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. 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 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. 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. 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. Sed-e '1c \#! /Bin/more 'example ----- (Replace the first line #! /Bin/more) sed-e's/word1/& word2/'example, the function parameter s has two special symbols: &: Representing pattern
Insert: After the row and before the row add new row: sed's/pattern/& \ n/G' filename before the row: sed's/pattern/\ n &/G' filename & indicates that pattern inserts the text sed-I '1 I \ into the string 'filename inserts sed-I 'in the last line before the first line' $ a \ insert string 'filename' insert sed-I '/pattern/I "insert string"' filename insert sed-I '/pattern/a "Insert before matching row string "'filename