Linuxshell script the Egg ache sed introduction Regular expression

Source: Internet
Author: User
Tags character classes line editor

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:--express Ion=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 file and then modify, the format is as follows: # sed-i.bak ' 1d ' filen Ame-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:  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:         copies the contents of the current hold buffer to the pattern space, replacing the contents of the current row in the pattern space g:         appends the contents of the current hold buffer to the pattern space, appends to the current line of the pattern space, and h:        copies the contents of the schema space into the current hold buffer. Clears the contents of the original staging buffer, adds new content h:        copies the contents of the schema space into the current hold buffer, and appends the contents of the remaining buffer to the previous i\:        inserts one or more lines of text before the anchor line number. l:        lists the contents of the current pattern space in a visible, strict form n:        Reads the next input line, n:        the next input line to the pattern space with the next command to process the new row. p:        Print Match 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 from the file s:        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 the last line of the input is read, even if there is no tag to the end of the script t label:  If the s instruction fails with a replace operation, it jumps to the place where the ":" tag, even if the last line of input has been read, and if there is no mark then the end of the script w:        Writes the entire contents of the current pattern space to a file w:        writes the first line in the current pattern space to a file x:         Swap mode space and text content y:        convert characters to another character (but not to regular expressions):  label:  establishing a reference position {} :      command group with the same address parameters #:         =:        Displays the file line number before extending the comment to the next newline character!:         command that does not execute a selected row, applies commands only to rows other than the selected row


Meta-character set

SED supports special meta-characters for pattern lookups and replacements for ^:           line header locators. such as:/^doiido/matches all lines beginning with Doiido $:           end-of-line locator. such as:/doiido$/matches all lines ending with Doiido ^$:          represents a blank line .:            matches characters other than line breaks. If:/d...do/  matches D followed by 3 arbitrary characters, then do. A *:           matches 0 or more leading characters. If:/doiido*/  matches all lines starting with Doiid, followed by 0 or more o []:          Matches any character within the specified character group. If:/[dd]oiido/  matches any of the characters that contain Doiido or Doiido, the [^]:         matches any character that is not within the specified character group. such as:/[^dd]oiido/  matches all lines that do not start with D or D but end with Oiido. \):      to save the matched characters. such as: S/\ (hello\) doiido/\1baby  here to save Hello as tag 1, if found Hellodoiido is replaced by Helloboy, 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--\<:          Initial locator. such as:/\<doiido/  matches all lines that contain words that begin with Doiido.  \>:          suffix Locator. such as:/doiido\>/  matches all lines that contain words ending in doiido. x\{m\}:      a continuous m x. If:/2\{8\}/  matches all rows that contain 8 consecutive 2.  x\{m,\}:     at least m x. If:/2\{8,\}/  matches all rows that contain at least 8 consecutive 2. x\{m,n\}:    at least m x, but not more than N. such as:/2\{6,8\}/  matches all rows that contain a continuous 6 to 8 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 digit [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 a line contains any number of digits [0-9][0-9]*$:     indicates that the end of the row 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 line s/^.//:           delete the first character at the beginning of a line s/^\///:           Delete the "/" character at the beginning of the line s/sp\ (.. \)//g:    Delete the character "SP" and the two arbitrary characters immediately following it, "splly" and "Y" ^#/:               matches any line that starts with   ' # '  /}^/ :             matches any line that ends with   '} ' (no spaces)/} *^/ :           matches any line with   '} '   followed by   0 or more spaces  /[abc]/ :          matches any line that contains lowercase   ' A ', ' B '  , or   ' C '  /^[abc]/ :        will match any line starting with   ' A ', ' B '   or   ' C '   


Positional parameters:
By default, commands used in the SED editor are applied to all text data rows.
If you want to apply a command only to a particular data row or to a set of text data rows, you must use row addressing.
Line 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 for rows that contain patterns/ The pattern/pattern/query contains a row of two patterns/pattern/,x the line from the matching line of pattern to the row of the X line x,/pattern/from line x to the line x that matches the pattern.                   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 SED use 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



This article is from the "lake and Laughter" blog, please make sure to keep this source http://hashlinux.blog.51cto.com/9647696/1795866

Linuxshell script the Egg ache sed introduction Regular expression

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.