Sed command details & regular Expressions __ Regular expressions

Source: Internet
Author: User
1. Introduction sed is a non-interactive editor. It does not modify the file unless you use Shell redirection to save the results. By default, all output lines are printed to the screen. The SED editor processes files (or inputs) line-by-row and sends the results to the screen. The process is as follows: First, SED saves the rows currently being processed in a temporary buffer (also known as the mode space) and then processes the rows in the temporary buffers and sends the line to the screen when it is finished. SED deletes it from the temporary buffer each time it finishes processing, and then reads the next line, processes and displays it. When the last line of the input file is processed, the SED ends.   SED has a temporary buffer in each row and edits the copy, so the original file is not modified. 2. The addressable addressing is used to determine which rows to edit. The form of an address can be a number, a regular expression, or a combination of both.   If no address is specified, SED processes all rows of the input file. The address is a number, the line number is the "$" symbol, and the last line is represented. For example:

Sed-n ' 3p ' datafile
Print only the third line

Displays only the contents of the file for the specified row range, for example: # View only rows 100th through NO. 200 of a file
Sed-n ' 100,200p ' Mysql_slow_query.log

Addresses are comma-delimited, the addresses that need to be processed are the ranges between the two lines, including these two lines. Scopes can be represented by numbers, regular expressions, or a combination of both. For example:

Sed ' 2,5d ' datafile
#删除第二到第五行
Sed '/my/,/you/d ' datafile
The line between the line #删除包含 "my" and the line that contains "you"
Sed '/my/,10d ' datafile
#删除包含 the contents of line to line tenth of "my"

3. Commands and Options

The sed command tells SED how to handle each input line specified by the address, and if no address is specified, all input lines are processed.

3.1 sed command

Command Function
A\

Adds one or more rows after the current row. Multiple rows with a "\" continuation at the end of each line except the last line

C\ Replaces the text in the current line with the new text after this symbol. Multiple rows with a "\" continuation at the end of each line except the last line
I\ Inserts text before the current line. Multiple rows with a "\" continuation at the end of each line except the last line
D Delete Row
H Copy the contents of the pattern space to the staging buffer
H Append the contents of the pattern space to the staging buffer
G Copy the contents of the staging buffer into the mode space, overwriting the original content
G Appends the contents of the staging buffer to the pattern space, appended to the original content
L List nonprinting characters
P Print lines
N Reads the next input line and starts processing it from the next command rather than the first command
Q End or exit sed
R Reading input rows from a file
! Apply a command to all rows except the selected line
S Replaces one string with another
G Global substitution within a row
W Writes the selected row to a file
X Exchange staging buffer and the contents of the schema space
Y Replace a character with another character (you cannot use the Y command on a regular expression)

3.2 sed option

Options Function
-E For multiple edits, that is, when you apply multiple SED commands to an input line
-N Cancel the default output
-F Specifies the filename of the SED script
4. Exit status sed does not like grep, its exit status is 0, regardless of whether the specified pattern is found. The exit state of SED is not 0 unless there is a syntax error in the command. 5. Regular expression metacharacters like grep, SED also supports special meta characters for pattern lookup and substitution. The difference is that the regular expression used by SED is the pattern that surrounds the slash line "/". If you want to change the regular expression delimiter "/" to another character, such as O, just add a backslash before the character, follow the regular expression after the character, and then follow the character. For example: Sed-n ' \o^myop ' datafile
Metacharacters Function Example
^ Header Locator /^my/matches all rows that start with my
$ Line End Locator /my$/matches all rows ending with my
. Matches a single character except a newline character /M.. y/match contains the letter m, followed by two arbitrary characters, and then with the letter Y line
* Match 0 or more leading characters /my*/matches a line containing the letter m, followed by 0 or more Y letters
[] Matches any character within a specified set of characters /[mm]y/matches the line that contains my or my
[^] Matches any character not within the specified set of characters /[^mm]y/matches a line that contains Y, but before y, that character is not m or m
Save matched characters 1,20s/self/\1r/the pattern between the meta characters and saves it as label 1, which can then be referenced using \1. You can define up to 9 labels, numbering from the left, and the first on the left. In this case, the 1th to the 20th row is processed, you are saved as label 1, and if youself is found, replace with your.
& Save a lookup string to reference in a replacement string s/my/**&**/Symbol & Representative lookup string. My will be replaced by **my**
\< Word First Locator /\<my/matches a line containing a word that starts with my
\> Final Locator character /my\>/matches a line that contains a word ending with my
X\{m\} Continuous m x /9\{5\}/Match contains 5 consecutive rows of 9
X\{m,\} At least m x /9\{5,\}/matches a row containing at least 5 consecutive 9
X\{m,n\} At least m, but not more than n x /9\{5,7\}/match contains rows of 5 to 7 consecutive 9
6. The example 6.1 P command command p is used to display the contents of the mode space. By default, sed prints the input lines on the screen, and option-n is used to cancel the default print operation. When the item-N and command p appear simultaneously, sed prints the selected content.

Sed '/my/p ' datafile
#默认情况下, sed prints all input lines on the standard output. If a row matches the pattern my,p command will print the line another time.


Sed-n '/my/p ' datafile
#选项-N cancels the SED default print, and the p command prints the matching mode my line again.

6.2 d Command

Command d is used to delete input rows. Sed first copies the input rows from the file into the pattern space, then executes the SED command on the row, and finally displays the contents of the pattern space on the screen. If the command d is emitted, the input line in the current mode space

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.