About the edit command SED

Source: Internet
Author: User

From: http://www.pczpg.com/html/caozuoxitong/Lunix_Unix/20090610/6404_4.html

Sed introduction:

 

Sed is a streamlined, non-interactive editor. It can execute the same editing task as editing VI and Emacs. Sed editor does not provide interactive usage: You can only enter the edit command on the command line, specify the file name, and view the output on the screen. Sed editor is not destructive. It does not modify the file, unless it uses shell redirection to save the output results. By default, the output is printed to the screen.
Sed editor is very useful in shell scripts, because the use of interactive editors such as VI or Emacs in shell scripts requires script users to be proficient in this editor, in addition, the user does not need to modify the opened file. If you want to execute multiple editing tasks or do not want to quote sed commands on the shell command line, you can also write the SED command in a file called sed script. Remember, shell will try to convert all the metacharacters or spaces in the command when entering the command on the command line. All characters in the SED command that may be interpreted by shell must be enclosed in quotation marks.
Sed is an online editor that processes a row of content at a time. During processing, the currently processed rows are stored in the temporary buffer, called the pattern space. Then, the SED command is used to process the content in the buffer, send the buffer content to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output. Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, and write conversion programs. The following describes the GNU sed 3.02 version.

1. Addressing
The address can be used to locate the row you want to edit. The address is composed of numbers, the two rows separated by commas indicate the range of rows starting and ending with the two rows (including the rows indicated by the number of rows ). For example, 1 and 3 indicate 1, 2, 3 rows, and the dollar sign ($) indicates the last row. The range can be determined by data, regular expressions, or a combination of the two.

2. Format of SED command call
The SED command can be called in two forms:
* Sed [Options] 'command' file (s)
* Sed [Options]-f scr extends ptfile file (s)
A/Add a line of text behind the current line.
B lable branch to the marked place in the script. If the branch does not exist, it is routed to the end of the script.
C/use new text to change the text of this line.
D. Delete the row from the position of the template block (pattern space.
D. Delete the first line of the template block.
I/Insert text on the current row.
H. Copy the content of the template block to the buffer in the memory.
H append the content of the template block to the buffer in the memory.
G obtains the content of the memory buffer and replaces the text in the current template block.
G to obtain the content of the memory buffer and append it to the end of the block Text of the current template.
L The list cannot print the character list.
N. Read the next input line and use the next command to process the new line instead of the first command.
N append the next input row to the template block and embed a new line between them to change the number of the current row.
P prints the row of the template block.
P (uppercase) prints the first line of the template block.
Q: Exit sed.
R file reads rows from the file.
T label if branch, starting from the last line, once the condition is met or t, t command, will lead to branch to the command with the label, or to the end of the script.
T label indicates the branch with an error. Starting from the last line, once an error occurs or the T and T commands are executed, the Branch is routed to the command with a label or to the end of the script.
W file write and append the template block to the end of the file.
W file write and append the first row of the template block to the end of the file.
! Indicates that the subsequent commands will work on all unselected lines.
S/RE/string Replace the regular expression re with string.
= Print the current row number.
* Extend the annotation to the next line break.
Replace tags
* G indicates that all rows are replaced.
* P indicates printing rows.
* W indicates writing rows into a file.
* X indicates the text in the SWAp template block and the text in the buffer.

* Y indicates to translate a character into another character (but not for a regular expression)

3. Option-e command, -- Expression = command
Multiple edits are allowed.
-H, -- help: print the help and display the address of the Bug list.
-N, -- quiet, -- silent cancel the default output.
-F, -- filer = scr then PT-file: the name of the SED script file under guidance.
-V, -- version: print the version and copyright information.

4. metacharacters
^ Start of the anchor row, for example:/^ SED/matches all rows starting with sed.
$ The End Of The Anchor row is as follows:/SED $/matches all rows ending with sed.
. Match a non-linefeed character such as:/S. D/match s followed by any character, then D.
* Match zero or multiple characters, such as:/* SED/match all templates with one or more spaces followed by sed rows.
[] Matches a character in a specified range, such as/[ss] ED/matches SED and sed.
[^] Match a character that is not within the specified range, for example:/[^ A-RT-Z] ED/match a line that is followed by a letter that does not contain a A-R and a T-Z.
/(../) Saves matching characters, such as S // (Love/) able // 1RS, and loveable is replaced with lovers.
& Save the search characters to replace other characters, such as S/love/** & **/. The love character is ** love **.
/<Specifies the start of a word, such as: // <love/matches a row that contains a word starting with love.
/> Pin the end of a word, such as/love/>/match the row containing the word ending with love. X/{M/} repeated characters X, m times, such as:/0/{5/}/matched rows containing 5 o.
X/{M,/} repeat character X, at least m times, such as:/O/{5, //}/matched rows with at least 5 o. X/{M, N/} repeats the character X, at least m times, no more than N times, for example,/O/{5, 10/}/matches rows of 5--10 O.

5. Instance
5.1 Delete: D command
* $ Sed '2d 'example ----- Delete the second line of the example file.
* $ Sed '2, $ d' example ----- delete all rows from the second row to the end of the example file.
* $ Sed '$ d' example ----- Delete the last row of the example file.
* $ Sed '/test/'d example ----- delete all rows containing test in the example file.
5.2 Replace: s command
* $ SED's/test/mytest/G' example ----- replace test with mytest in the entire row. If no G tag exists, only the first matched test in each row is replaced with mytest.
* $ Sed-n's/^ test/mytest/P' example ----- (-N) option and the P Flag are used together to print only the replaced rows. That is to say, if the test at the beginning of a row is replaced with mytest, print it.
* $ SED's/^ 192.168.0.1/& localhost/'example ----- & symbol represents the part found in the replacement string. All rows starting with 192.168.0.1 are replaced with their own localhost and changed to 192.168.0.1localhost.
* $ Sed-n's // (Love/) able // 1RS/P' example ----- love is marked as 1, and all loveable is replaced with lovers, the replaced line is printed out.
* $ SED's #10 #100 # g'example ----- whatever the character, followed by the S command is considered as a new separator. Therefore, "#" is a separator here, replaces the default "/" separator. Replace all 10 with 100.
5.3 range of selected rows: comma
* $ Sed-n'/test/,/check/P' example ----- all rows within the range specified by the template test and check are printed.
* $ Sed-n'5,/^ test/P' example ----- print all rows starting from the fifth line to the first line containing the start of test.

* $ Sed '/test/,/check/S/$/SED test/'example ----- for the rows between the template test and west, the end of each row is replaced by the string sed test.
5.4 multi-point Editing: e command
* $ Sed-e '1, 5d '-E's/test/check/'example ----- (-e) options allow multiple commands to be executed in the same line. As shown in the example, the First Command deletes lines 1 to 5, and the second command replaces test with check. The command execution order has an impact on the result. If both commands are replacement commands, the first replacement command will affect 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 SED expressions.
5.5 read from file: R command
* $ Sed '/test/R file' example ----- the content in file is read and displayed after the row matching test. If multiple rows match, the file content is displayed under all matched rows.
5.6 file writing: W command
* $ Sed-n'/test/W file' example ----- all rows containing test in example are written to file.
5.7 APPEND Command: a command
* $ Sed '/^ test/A // ---> This Is A example 'example <----- 'this is a example' is appended to the end of the row starting with test, sed requires that command a be followed by a backslash.
5.8 insert: I command
$ Sed '/test/I //
New Line
------------------------- 'Example
If test is matched, the text following the backslash is inserted before the matching row.
Next: N command
* $ Sed '/test/{n; S/AA/BB/;}' example ----- if test is matched, move it to the next row of the matched row and replace AA in this row, change to BB, print the row, and continue.
5.9 Deformation: y command
* $ Sed '1, 10y/ABCDE/'example ----- converts all ABCDE in line 1-10 to uppercase. Note that this command is not applicable to the metacharacters of regular expressions.
5.10 Exit: Q command
* $ Sed '10q' example ----- exit sed after printing the 10th rows.
5.11 maintain and obtain: H and G commands
* $ Sed-e '/test/H'-e' $ g example ----- when sed processes files, each row is saved in a temporary buffer called a mode space, all processed rows are printed on the screen unless the row is deleted or the output is canceled. The mode space is cleared, and a new row is saved for processing. In this example, the row Matching Test is found and saved to the mode space. The H Command copies the row and saves it to a special buffer zone called the guaranteed cache. The second statement means that when the last line is reached, the G command extracts the row that maintains the buffer and places it back in the mode space, and append it to the end of the row that already exists in the mode space. In this example, It is appended to the last row. Simply put, any row containing test is copied and appended to the end of the file.
5.12 maintain and swap: H command and X command
* $ Sed-e '/test/H'-e'/check/X' example ----- swap mode space and keep the buffer content. That is, to swap the rows containing test and check.

6. Script
Sed script is a list of SED commands. When SED is started, the file name of the script is guided by the-F option. Sed is very picky about the commands entered in the script. There cannot be any blank or text at the end of the command. If there are multiple commands in one line, separate them with semicolons. Comments rows starting with # And cannot span rows.

 

7. Tips

* Use double quotation marks when referencing shell variables in the SED command line, instead of the common single quotation marks. The following is a script to delete the zone segment in the named. conf file based on the content of the name variable:
Name = 'zone/"localhost "'
Sed "/$ name/,/};/D" named. conf

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.