Sed is a stream editor, which is a very useful tool in text processing and can be used perfectly in conjunction with regular expressions. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.
-sed options, commands, replacement tags
Command format
sed [options] ' command ' file (s) sed [options]-F scriptfile file (s)
Options
-e<script> or--EXPRESSION=<SCRIPT>: processes the input text file with the specified script in the options;-f<script file > or--file=<script file : Process the input text file with the script file specified in the options,-H or--help: Display Help,-N or--quiet or--silent: Displays only the results of script processing;-V or--version: Displays version information.
SED command
a\ insert text below the current line. i\ inserts text above the current line. c\ changes the selected line to a new text. d Delete, delete the selected row. d Delete the first line of the template block. s replaces the specified character h copies the contents of the template block into an in-memory buffer. h appends the contents of the template block to the in-memory buffer. g gets the contents of the memory buffer and overrides the text in the current template block. g gets the contents of the memory buffer and appends it to the text of the current template block. The l list cannot print a list of characters. n reads the next input line and uses the next command to process the new row instead of the first command. n appends the next input line to the template block and embeds a new line between them, changing the current line number. p the line that prints the template block. P (uppercase) Print the first line of the template block. q exit sed. b lable branches to the markup in the script, branching to the end of the script if the branch does not exist. r file reads rows from file. T label if Branch, starting with the last line, the condition satisfies or t,t the command, causing the branch to be at the command with the label, or at the end of the script. t label the wrong branch, starting with the last line, when an error or T,T command occurs, it causes the branch to be at the command with a label, or to the end of the script. w file writes and appends the template block to the end of file. W file writes and appends the first line of the template block to the end of file. ! indicates that subsequent commands do not work for all rows that are not selected. = prints the current line number. # extends annotations to the next line break.
SED replacement flag
G indicates a full-line replacement within the row. P indicates that the line is printed. W means writing the line to a file. X represents the interchange of text in the template block and the text in the buffer. Y means translating one character to another (but not for regular expressions \1 substring match tag & matched string tag
Sed meta Character set
^ Match line starts, such as:/^sed/matches all lines beginning with sed. The $ match line ends, such as:/sed$/matches all lines ending in sed. Any character that matches a non-line break, such as:/s.d/matches S followed by an arbitrary character, and finally D. * Match 0 or more characters, such as:/*sed/match all the templates are one or more spaces followed by the SED line. [] matches a specified range of characters, such as/[ss]ed/-match sed and sed. [^] matches a character that is not within the specified range, such as:/[^a-rt-z]ed/matches the beginning of a letter that does not contain a-r and t-z, followed by the line of Ed. \(.. \) match substring, save matching characters, such as s/\ (love\) able/\1rs,loveable is replaced with lovers. & Save Search characters to replace other characters, such as S/love/**&**/,love this into **love**. \< matches the beginning of a word, such as:/\<love/matches a line containing a word that begins with love. \> matches the end of a word, such as/love\>/matches a line containing a word ending in love. X\{m\} repeats characters x,m times, such as:/0\{5\}/matches a row containing 5 0. X\{m,\} repeats the character x, at least m times, such as:/0\{5,\}/matches at least 5 rows of 0. X\{m,n\} repeats the character x, at least m times, not more than n times, such as:/0\{5,10\}/matches 5~10 0 rows.
-sed usage Examples
Replace operation: s command
# Replace the string in the text in the $ sed ' s/book/books/' files#-n option and the P command to use a representation to print only those occurrences of the substituted row $ Sed-n ' s/book/books/' P files# direct edit File Option-I, Matches the first book of each row in the file to replace the books$ sed-i ' s/book/books/' files
Full Replacement Mark G
# using the suffix/g tag will replace all matches in each row with the $ sed ' s/book/books/g ' files# you can use/ng$ sed-n ' s/book/books/2g ' p files when you need to start replacing from the nth of each line
Delimiter
# before the characters in the command/used as delimiters in sed, you can also use any delimiter $ sed ' s#book#books# ' files $ sed ' s|book|books| ' files$ sed ' s:book:books: ' Files # Set You need to escape the $ sed ' s/\/user\/bin/#user #bin/' files when it appears inside a style
Delete operation: D command
# Delete blank line: $ sed '/^$/d ' files# delete file on line 2nd: $ sed ' 2d ' files# delete file 2nd line to end all lines: $ sed ' 2, $d ' files# delete file last line: $ sed ' $d ' files# delete file All lines in the beginning are test: $ sed '/^test/' d files
Matched string Markers &
# Regular Expression \w\+ matches each word, uses--&--to replace it,& corresponds to the word that was previously matched # run to see what the effect is different $ sed ' s| Ab|--&--|g ' files$ sed ' s|\w|--&--|g ' files$ sed ' s|\+|--&--|g ' files$ sed ' s|\w\+|--&--|g ' files# all with Boo The lines beginning with k are replaced with [their own +& character strings (including spaces)]$ sed ' s|^book|& test|g ' files
Combining multiple expressions
# 3 command execution results are equivalent to sed ' s/book/books/' files | Sed ' s/books/book/' $ sed ' s/book/books/' | Sed ' s/books/book/' files$ sed ' s/book/books/;s/books/book/' files
Reference
# sed expressions can be quoted using single quotes, but if the expression contains variable strings inside, you need to use double quotation marks $ test= "Teh is TEST." $ sed "s/test/$TEST/" Files
The range of the selected row, (comma)
# All lines within the range determined by the template books and Test are printed $ Sed-n '/books/,/test/p ' files# printing starts from line 3rd to the first containing all lines between the lines starting with test $ Sed-n ' 3,/^test/p ' files # for the line between the template books and Test, the end of each line is replaced by the string look books the $ sed '/books/,/test/s/$/look books/' files
Multi-point editing: E command
# Multi-point editing: Delete the rows and replace the Test string with check$ sed-e ' 1,2d '-e ' s/test/check/' files
Read from File: R command
# The contents of the file are read in and displayed after the line matching the Test, and if multiple lines are matched, the contents of file will be displayed under all matching lines below $ sed '/test/r file ' files
Write file: w command
# All rows containing Test in files are written to file $ Sed-n '/test/w file ' files
Append (line): A\ command
# Append the IS-a test line to the row that begins with test $ sed '/^test/a\the is a test line ' files insert the is a test line$ sed ' 4a before the 4th row of the files file \the is a Test line ' files
Next: N command
# If book is matched, move to the next line of the matching line, replace the test on this line, change to test, and print the line, then continue with the SED '/book/{n; s/test/test/g;} ' files
Deform: Y command
# turn all the book in the row into uppercase. Note: Regular expression metacharacters cannot be used with this command $ sed ' 1,3y/book/book/' files
Exit: Q command
# After printing line 10th, exit sed$ sed ' 4q ' files
I want to add ...
This article is from the "Destiny." blog, be sure to keep this provenance http://hypocritical.blog.51cto.com/3388028/1763029
The application of the SED command in the shell Three Musketeers