Introduction to SED commands and how to use variables in sed commands

Source: Internet
Author: User

1. Introduction to SED
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.
2. 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.

3. Sed command
The SED command can be called in two forms:
*
Sed [Options] 'command' file (s)
*
Sed [Options]-F scriptfile file (s)
A \ (backslash)
Add a line of text behind the current line.

I \
Insert text on the current row. C \ use new text to change the text of this line.

B lable
Branch to the marked place in the script. If the branch does not exist, branch to the end of the script.
D
Deletes a row from the position of the template block (pattern space.
D
Delete the first line of the template block.
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
Obtain the content of the memory buffer and replace the text in the current template block.
G
Obtain the content of the memory buffer and append it to the end of the current template block text.
L
List of characters that cannot be printed.
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 end of the template block and embed a new line between them to change the number of the current row.
P
Print the row of the template block.
P (uppercase)
Print the first line of the template block.
Q
Exit sed.
R File
Read rows from the file.
T label
If branch, starting from the last line, once the condition is met or the T, T command will lead to branch to the command with a label or to the end of the script.
T label
An error branch starts from the last line. If an error occurs or the T or t command, 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'
Use string to replace the regular expression re.
=
Print the number of the current row.
#
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 translating a character into another character (but not used in regular expressions)

4. Options
-E command, -- Expression = command
Multiple edits are allowed.
-H, -- Help
Print help and display the address of the Bug list.
-N, -- quiet, -- silent

Cancel default output.
-F, -- filer = script-File
The name of the bootstrap sed script file.
-V, -- version
Print the version and copyright information.

5. metacharacters

^
The start of the anchor row is as follows:/^ 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, for example,/S. D/match s, followed by any character, and 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, such as:/[^ A-RT-Z] ED/match a line that is followed by a letter that does not contain a A-R and a T-Z.
\(..\)
Save matched 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 **.
\ <
Anchor specifies the start of a word, for example,/\ <love/matches a row that contains a word starting with love.
\>
Anchor specifies the end of a word, for example,/love \>/matches a row containing a word ending with love.
X \ {M \}
Repeat the characters X and M, for example,/0 \ {5 \}/matches the rows containing 5 o.
X \ {M ,\}
Repeated characters X, at least m times, such as:/O \ {5, \}/matched rows with at least 5 o.
X \ {M, N \}
Repeated characters X, at least m times, no more than N times, such as:/O \ {5, 10 \}/rows matching 5-10 o.
6. Instance
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.
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 indicates replacing the part found in the 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 will be 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.
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 that contains the beginning 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.


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.


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.


Write File: W command
$ Sed-n'/test/W file' example ----- all rows containing test in example are written to file.


APPEND Command: command
$ Sed '/^ test/A \ This is an example' filename ----- This Is An example is appended to the end of the row starting with test in the filename file, sed requires that command a be followed by a backslash.

 

Insert: I command

$ Sed '/test/I \ new line' filename

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 of this row, change to BB, print the row, and continue.


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 regular expression metacharacters.


Exit: Q command

$ Sed '10q' example ----- exit sed after printing the 10th rows.


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.
Persistence and interchange: H and X commands
$ 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.
7. 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.

 

Ii. using external variables in the SED command:

1. When the SED command uses double quotation marks, use $ VaR to directly reference

$ Echo | sed "s/^/$ random. rmvb _/G"
29328. rmvb _

# The preceding example references the value of an environment variable $ random.

2. When the SED command uses single quotes, use '"$ Var"' to reference

Similarly, we can see that

$ Echo | SED's/^/'"$ random"'. rmvb _/G'
31338. rmvb _

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.