about SED
SED is a lightweight flow editor, shorthand for stream editor. Because SED is an edit file in the behavior unit, it is also called the line editor. It automates editing by eliminating the need to edit data directly.
How SED works:
Read the edit file by the standard input, read one line or specify the line into the mode space, then edit the contents of the mode space one by one, then output the result to standard output, and clear the mode space. The next line of data is then read into the pattern space, so the previous operation is repeated until the last line, and the stream editor stops. The source file does not change
Ps:
1, the mode space for reading in the cache, the SED to the text line processing is carried out here
2. One line at a time, apply the command line by row
3, sed command execution and return data almost at the same time, the processing of each row of data will be displayed at the same time the results
4, the file content exists mode space, and has not changed, unless the use of redirected storage output, so the original file will not be modified
Grammar:
Made up of edit directives and files
1.# sed [sed options] ' sed command ' to modify files > new files
2.# sed [sed option]-f SED script file to be modified
3.# sed script [sed options] file to be modified
Parameters (sed option):
-e Command:--expression=command: Multiple edits are made to resolve subsequent strings to the SED edit command, which is used when multiple SED commands are applied to the input line. # sed-e ' 1,5d '-e ' s/boy/girl/' Dodo # sed--expression= ' 1,5d '--expression= ' s/boy/girl/' dodo-e script-file:-- Expression=script-file: Invokes the specified SED script file to process the input text file-F script-file:--filer= Script-file: Invokes the specified SED script file to process the input text file-h:--help: Print help-I: Directly modify the Read source file (default does not modify the source file) can also back up the source files and then modify, the format is as follows: # sed- I.bak ' 1d ' filename-n:--quiet,--silent: Cancels the default output, prints only rows that match the pattern (default output All) the-r:sed action supports the syntax of the extended formal notation. (the default is the normal notation of French)-v:--version: Display version information
sed command
A\: adds one or more lines of text after the anchor line number. (When multiple rows are added, the last line ends with a "\" continuation) b lable: Jumps the executed instruction to the reference location established by ":" and jumps to the end of the script if no reference position exists C\: replaces the text of the anchor line with the new text. D: Delete anchor row d: Delete the first line in the pattern space G: Copy the contents of the current hold buffer to the pattern space, replacing the contents of the current row in the pattern space g: Append the contents of the current hold buffer to the pattern space, Appended to the current line of the pattern space H: Copies the contents of the schema space into the current hold buffer, clears the contents of the original staging buffer, and adds new content h: Copies the contents of the schema space into the current hold buffer, appended to the i\ after preserving the original contents of the buffer: Inserts one or more lines of text before the anchor line number. L: List The contents of the current pattern space in a visible, strict form N: reads the next input line and processes the new row n with the next command: appends the next input line to the pattern space. P: Print matching line p: The first line of the print mode space Q: exit Sedr file when the first pattern match is completed: reads the input line s from the files: replaces another string with one. (The default is to replace the first one in each row) T label: If the s instruction occurs successfully, then jumps to the ":" Mark where, even if the last line of the input has been read, if there is no mark then to the end of the script T label: If the s instruction fails with a replace operation, it jumps to the ":" Mark, Even if the last line of input is already read, if there is no mark then the end of the script W: writes the entire contents of the current pattern space to the file W: writes the first line in the current pattern space to the file x: swap mode space and text content y: Converts a character to another character (but not to a regular expression): Label: Establish reference location {}: instruction group with the same address parameter #: before extending the comment to the next newline character =: Displays the file line number!: Commands that do not execute a selected row, apply commands only to rows other than the selected row
Meta Character Set
SED supports special meta-characters for pattern finding, substitution ^: line header. For example:/^doiido/matches all lines starting with Doiido $: line tail locator. For example:/doiido$/matches all lines ending with Doiido ^$: represents a blank line.: matches characters other than line breaks. For example:/d...do/matches D followed by 3 arbitrary characters, then do. *: matches 0 or more leading characters. such as:/doiido*/matches all lines beginning with doiid, followed by 0 or more o rows []: matches any character within the specified character group. such as:/[dd]oiido/matches all rows that contain Doiido or Doiido [^]: matches any character that is not within the specified character group. For example:/[^dd]oiido/matches all lines that do not start with D or D but end with Oiido. \): saves the matched characters. such as: S/\ (hello\) doiido/\1baby here to save Hello as a label 1, if found Hellodoiido is replaced by Helloboy, you can define up to 9 tags &: save find string to replace other strings. such as: s/doiido/--&--/, Symbol & representative find string, Doiido will become--doiido--\<: Word Header locator. For example:/\<doiido/matches all lines that contain words that begin with Doiido. \>: suffix locator. For example:/doiido\>/matches all lines that contain words ending with doiido. X\{m\}: a continuous m x. For example:/2\{8\}/matches all rows containing 8 consecutive 2. X\{m,\}: at least m x. such as:/2\{8,\}/matches all rows that contain at least 8 consecutive 2. x\{m,n\}: at least m x, but not more than N. For example:/2\{6,8\}/matches all rows containing 6 to 8 consecutive 2.
Ps: Match meta character $ before you must use a backslash \ mask its special meaning. Like/\$/.
Character class Extensions:
[]: can be used in conjunction with "-" [A-z]: match all lowercase letters [0-9]: match all the numbers [: space:]: match spaces [: Alnum:]: match alphanumeric [A-Z-0-9][: Alpha:]: match letter [A-Z A-z][:blank:]: match space or TAB key [: Cntrl:]: match any control character [:d Igit:]: match number [0-9][:graph:]: Match any visual character (no spaces) [: Lower:]: match lowercase [a-z][:p rint:]: match non-control character [:p UNCT:]: Match punctuation character [: space:]: match space [: Upper :]: match uppercase [A-z][:xdigit:]: match hexadecimal number [0-9 a-f a-f]
Examples of character classes:
^[0-9]: indicates that the first character of a row is any number ^[0-9]*: indicates that the beginning of the line contains any number of digits [0-9][0-9]*$: indicates that the end of the line contains at least 2 digits s/\.$//g: Delete the period of the line ending with a period s/^[[]*//: Delete any space at the beginning of the row s/^.//: Delete the first character of the beginning s/^\///: Delete the "/" character s/sp\ (. \)//g: Delete the character "SP" and the two arbitrary characters immediately following it, "splly"-"Y" ^#/: matches any line starting with ' # '/}^/: matches any line with '} ' (no spaces)/} *^/: matches any line that ends with 0 or more spaces followed by '} '/[abc]/: will match any row containing lowercase ' a ', ' B ' or ' C ': will match any line starting with ' A ', ' B ' or ' C '
Position parameters:
By default, commands used in the SED editor are applied to all text data rows.
If you only want to apply a command to a particular data row or a set of text data rows, you must use row addressing.
Row addressing locates the rows you wish to edit by addressing them, either by data, regular expressions, or by combining them. There are several formats for row addressing:
x x for the specified line number the last line, X, y specifies the range of line numbers from X to y/pattern/query the row containing the pattern/pattern/pattern/ query contains rows with two patterns/pattern/,x From the line that matches the pattern to the row of the X line x,/pattern/the line from the X line to the matching line of pattern x,y! Lines that do not include X and Y line numbers
Number of address parameters:
When only 1 address parameters are available, only the rows that match the positional parameters are edited
When there are 2 address parameters, such as x, Y, represents an edit to the range data area of the line numbers from X to Y. (including x and y)
Quotation mark Syntax
1, the single quotation mark, the $ and after the quotation mark ' interpretation and execution, that is, the two months as ordinary characters
2. Under double quotation marks, the dollar symbol is expanded to the value of the variable or parameter, and the command in the post quotation marks is executed and replaced with the result of the output in the enclosed quotation mark.
So you usually use single quotes ", use double quotes when using variables" "
When using variables:# sed "/$hello/d" Dodo
sed exit Status:
1, regardless of whether the specified mode is found, the exit status is 0.
2. When there is a syntax error in the command, the exit status of SED is not 0.
Escape:
If you need to use a slash "/", you need to first transfer it, escaping generally have the following two kinds of
1:[/]
2:\/
Single line using multiple statements
1. Use a semicolon between commands
# sed-n ' =;p ' Dodo
2. Use curly Braces
# sed-n ' 3{
> =
> P} ' Dodo
3. Using the-e parameter
# sed-n-E ' = '-e ' P ' Dodo
4. Use a script file and then use the-f reference
# sed-f Scirpt Dodo
5, bash Shell can use the second prompt "
# sed-i '
>s/boy/girl/
>s/hello/byebye/' Data
Other SEDUse note:
1. If the address is not used, the command will be applied to all rows
2, the default will only replace the first occurrence of the text in each row, if there are more than one row to replace, you need to replace the tag
3, if you want to locate a special character, you must use "\" or "[]" to block its special meaning
======================== Script ==========================
Script: The sed script is a list of SED commands written in the file, with the-f option to boot the script file name when starting sed.
How the script works:
When executing a script, sed first copies the first line of the input file into the pattern space and then executes all the commands in the script. Once a line is processed, sed copies the next line of the file to the schema space and executes all the commands in the script. Until the last line.
Script notes:
1, in the script, at the end of the command can not have any blank or text
2. If there are multiple commands in a row, separate them with semicolons.
3. The behavior begins with # comment lines, and cannot span rows.
Script format:
#!/bin/sed-f
Command1
Command2
Script instance:
# Cat Test.sed
#!/bin/sed-f
/hello/a\
Hi Boy Doiido
2i\
you are great
Add Permissions and Execute
# chmod U+x test.sed
#./test.sed Dodo
Introduction to the Sed of Linux shell scripts regular expressions