"Turn" linux sed command in detail

Source: Internet
Author: User

Original URL: http://www.iteye.com/topic/587673

1. Introduction to SED
Sed is an online editor that processes a single line of content at a time. 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. The GNU version of SED 3.02 is described below.
2. Addressing
You can locate the row you want to edit by addressing it, which is made up of numbers, and two rows separated by commas represent the range of rows starting and ending with these two behaviors (including the two lines represented by the number of rows). The dollar sign ($) represents the last line, as 1,3 represents a one-line. The range can be determined by data, regular expressions, or by combining them.

3. SED command
There are two forms of calling the SED command:
*
sed [options] ' command ' file (s)
*
sed [options]-F scriptfile file (s)
A\
Adds a line of text after the current line.
b lable
Branch to the place marked in the script and branch to the end of the script if the branch does not exist.
C\
Change the text of the bank with new text.
D
Deletes a row from the template block (Pattern space) location.
D
Delete the first line of the template block.
I\
Inserts text above the current line.
H
Copies the contents of a template block into an in-memory buffer.
H
Append the contents of a template block to a buffer in memory
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 current template block text.
L
List cannot print a list of characters.
N
Reads the next input line, processing the new row with the next command 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
Prints the line of the template block.
P (uppercase)
Prints the first line of the template block.
Q
Exit sed.
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, will cause the branch to be at the command with a label, or at the end of the script, once an error or T,T command occurs.
W file
Write and append 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 the subsequent command has effect on all rows that are not selected.
S/re/string
Replace the regular expression re with string.
=
Prints the current line number.
#
Extends annotations to the next line break.
The following is the replacement tag
*
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 a character to another character (but not for regular expressions)

4. Options
-e command,--expression=command
Allows more than one edit.
-H,--help
Print Help and display the address of the bug list.
-N,--quiet,--silent

Cancels the default output.
-F,--filer=script-file
Boot sed script file name.
-V,--version
Print version and copyright information.

5. Meta Character Set ^
The beginning of the anchoring line is as follows:/^sed/matches all lines beginning with sed.
$
The end of the anchoring line is as follows:/sed$/matches all lines ending in sed.
.
Match a non-newline character such as:/s.d/matches the s followed by an arbitrary character followed by D.
*
Match 0 or more words 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.
\(.. \)
Save matching characters such as s/\ (love\) able/\1rs,loveable are replaced with lovers.
&
Saving search characters is used to replace other characters, such as S/love/**&**/,love, which becomes **love**.
\<
Anchors the beginning of a word, such as:/\<love/matches a line containing a word that begins with love.
\>
Anchors the end of a word, such as/love\>/, that matches a line containing a word ending in love.
X\{m\}
Repeat characters x,m times, such as:/0\{5\}/matches rows that contain 5 O.
X\{m,\}
Repeat the character X, at least m times, such as:/o\{5,\}/matches at least 5 rows of O.
X\{m,n\}
Repeat the character X, at least m times, not more than n times, such as:/o\{5,10\}/matches the line of 5--10 O.
6. Example
Delete: D command
*
$ Sed ' 2d ' example-----Delete the second line of the example file.
*
$ sed ' 2, $d ' example-----Delete the second line of the example file to the end of all lines.
*
$ sed ' $d ' example-----Delete the last line of the example file.
*
$ sed '/test/' d example-----Delete all rows containing test in example file.
Replace: s command
*
$ sed ' s/test/mytest/g ' example-----Replace test with mytest in the entire row range. If there is no G tag, only the first matching test of each row is replaced with mytest.
*
$ Sed-n ' s/^test/mytest/p ' example-----(-N) option and the P flag are used together to indicate that only those rows that have substitution occur are printed. That is, if the test at the beginning of a line is replaced with mytest, it is printed.
*
$ sed ' s/^192.168.0.1/&localhost/' example-----& symbol represents the part that is found in the replacement string. All lines starting with 192.168.0.1 will be replaced with a self-added localhost, which becomes 192.168.0.1localhost.
*
$ Sed-n ' s/\ (love\) able/\1rs/p ' Example-----Love is marked as 1, all loveable will be replaced with lovers, and the replacement guild is printed.
*
$ sed ' s#10#100#g ' example-----no matter what character, followed by the S command is considered a new delimiter, so, "#" Here is the delimiter, instead of the default "/" delimiter. means to replace all 10 with 100.
Range of selected rows: comma
*
$ Sed-n '/test/,/check/p ' example-----all lines in the range determined by the template test and check are printed.
*
$ Sed-n ' 5,/^test/p ' example-----prints all rows starting from line fifth to the first containing the line starting with test.
*
$ sed '/test/,/check/s/$/sed test/' example-----for the line between the template test and West, the end of each line is replaced with the string sed test.
Multi-point editing: E command
*
$ Sed-e ' 1,5d '-e ' s/test/check/' example-----(-e) option allows multiple commands to be executed on the same line. As shown in the example, the first command deletes rows 1 through 5, and the second command replaces test with a check. The order in which the commands are executed has an effect on the results. If all two commands are replacement commands, the first substitution command affects the result of the second replacement command.
*
$ sed--expression= ' s/test/check/'--expression= '/love/d ' example-----a better command than-E is--expression. It can assign values to an SED expression.
Read from File: R command
*
The contents of the $ sed '/test/r file ' example-----file are read in and displayed after the line matching the test, and if multiple lines are matched, the contents of file are displayed below all matching rows.
Write file: w command
*
$ Sed-n '/test/w file ' example-----All rows containing test in example are written to file.
Append command: A command
*
$ sed '/^test/a\\--->this is a example ' example<-----' This is a example ' appended to the line beginning with test, SED requires a backslash after command a.
Insert: I command
$ sed '/test/i\\
New Line
-------------------------' Example
If test is matched, the text that follows the backslash is inserted before the matching line.
Next: N command
*
$ sed '/test/{n; s/aa/bb/;} ' example-----if test is matched, move to the next line of the matching line, replace the AA for this line, change to BB, and print the line, and then continue.
Deform: Y command
*
$ sed ' 1,10y/abcde/abcde/' example-----convert all ABCDE in the 1--10 line to uppercase, note that the regular expression metacharacters cannot use this command.
Exit: Q command
*
$ sed ' 10q ' example-----after the 10th line has been printed out, SED is exited.
Hold and get: H command and G command
*
$ Sed-e '/test/h '-e ' $G example-----when the SED processes the file, each row is stored in a temporary buffer called the pattern space, unless the row is deleted or the output is canceled, and all the processed rows are printed on the screen. The pattern space is then emptied and a new line is stored for processing. In this example, when a row matching test is found, it is stored in the pattern space, and the H command copies it into a special buffer called the hold cache. The second statement means that when the last line is reached, the G command takes out the row holding the buffer, then puts it back into the pattern space and appends it to the end of the line that already exists in the pattern space. In this example, it is appended to the last line. Simply put, any line containing test is copied and appended to the end of the file.
Hold and Swap: H command and x command
*
$ Sed-e '/test/h '-e '/check/x ' example-----swap mode space and keep the contents of the buffer. That is, the line that contains test and check is swapped.
7. Scripts
The SED script is a list of SED commands that boot the script file name with the-F option when you start sed. Sed is very picky about the commands entered in the script, and cannot have any whitespace or text at the end of the command, separated by semicolons if there are multiple commands in a row. The behavior that begins with # comments lines, and cannot span rows.

Sed really can greatly improve our efficiency, the following write a line, a lot of files have been replaced, really convenient

Java code
    1. Sed ' s/localhost/127.0.0.1/g ' mysql_virtual_*.cf

"Turn" linux sed command in detail

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.