The linux sed command __linux

Source: Internet
Author: User
1. Sed introduction
Sed is an online editor that handles a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file. The contents of the file do not change unless you use redirected storage output. SED is used to automatically edit one or more files, to simplify the repetitive operation of files, to write conversion programs, and so on. The GNU version of SED 3.02 is described below.


2. Addressable
You can locate the row you want to edit by addressing it with numbers, with two rows separated by commas representing the range of rows that are the starting and ending lines of the two acts, including the two rows represented by the number of rows. If 1,3 represents a 1,2,3 line, the dollar sign ($) represents the last line. Scopes can be determined by data, regular expressions, or a combination of the two.


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 mark in the script and branch to the end of the script if the branch does not exist.
C\
Change the text of the line with the new text.
D
Deletes a row from the template block (pattern space) location.
D
Deletes the first line of the template block.
I\
Inserts text above the current line.
H
Copies the contents of the 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 replaces the text in the current template block.
G
Gets the contents of the memory buffer and appends it to the back of the current template block text.
L
List of characters cannot be printed on lists.
N
Reads the next input line and processes 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 the two, changing the current line number.
P
Print 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 at the last line, once the condition is met or the t,t command, it will cause branching to the labeled command, or to the end of the script.
T Label
The error branch, starting at the last line, causes an error or T,T command to branch to a labeled command, or to the end of the script.
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 the file.
!
Indicates that the following command has effect on all rows that are not selected.
S/re/string
Replaces the regular expression re with a string.
=
Prints the current line number.
#
Extend the annotation before the next line break.
The following is a replacement tag
*
G means full replacement in line.
*
P represents the print line.
*
W indicates that the line is written to a file.
*
X represents the interchange of text in the template block and the text in the buffer.
*
Y means translating a character into another character (but not for 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
Cancels the default output.
-F,--filer=script-file
boot sed script filename.
-V,--version
Print version and copyright information.


5. Meta Character Set ^
The beginning of the anchoring line, such as:/^sed/matches all rows beginning with sed.
$
The end of the anchoring row, such as:/sed$/matches all rows ending with sed.
.
Matches a newline character Furu:/s.d/matches S followed by an arbitrary character followed by D.
*
Match 0 or more characters Furu:/*sed/matches all the templates are one or more spaces followed by the SED line.
[]
Matches a specified range of characters, such as/[ss]ed/to match sed and sed.
[^]
Matches a character that is not in 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.
&
Save the search characters to replace other characters, such as S/love/**&**/,love this into **love**.
\<
Anchors the beginning of a word, such as:/\<love/matches a line that contains a word that begins with love.
\>
Anchors the end of a word, such as/love\>/to match a line that contains a word ending with love.
X\{m\}
Repeat characters x,m times, such as:/o\{5\}/matches rows containing 5 O.
X\{m,\}
Repeat character X, at least m times, such as:/o\{5,\}/matches a line with at least 5 O.
X\{m,n\}
Repeat character X, at least m times, no more than n times, such as:/o\{5,10\}/matches 5--10 o rows.


6. Examples


Delete: D command
*
$ Sed ' 2d ' example-----Delete the second line of the example file.
*
$ sed ' 2, $d ' Example-----Delete example files from the second line to the end of all rows.
*
$ sed ' $d ' example-----Delete the last line of 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 across the entire line. If there is no G-tag, only the first matching test for each row is replaced with mytest.
*
The $ Sed-n ' s/^test/mytest/p ' example-----(-N) option is used with the P flag to print only those lines that have been replaced. In other words, if 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 found in the replacement string. All rows beginning with 192.168.0.1 are replaced with localhost, which becomes 192.168.0.1localhost.
*
$ Sed-n ' s/\ (love\) able/\1rs/p ' Example-----Love is marked as 1, all loveable are replaced with lovers, and the replaced rows are 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 separator, instead of the default "/" separator. means to replace all 10 with 100.
Range of selected rows: comma
*
$ Sed-n '/test/,/check/p ' example-----all rows in the range determined by the template test and check are printed.
*
$ Sed-n ' 5,/^test/p ' example-----print all rows from line fifth to the first line that contains the start of test.
*
$ sed '/test/,/check/s/$/sed test/' example-----for the rows between the template test and West, the end of each line is replaced with the string sed test.


Multi-point edit: E command
*
$ Sed-e ' 1,5d '-e ' s/test/check/' example-----(-e) option allows multiple commands to be executed on the same line. As the example shows, the first command deletes 1 to 5 rows, and the second command replaces test with 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 a value to a SED expression.


Read from File: R command
*
$ The contents of the SED '/test/r file ' example-----file are read in, displayed after the row that matches the test, and if multiple rows are matched, the contents of file are displayed below all matching rows.


Write to 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 are a example ' is appended to the line beginning with test, and SED requires command A to have a backslash behind it.


Insert: I command
$ sed '/test/i\\
New Line
-------------------------' Example
If test is matched, the text that follows the backslash is inserted before the matching row.
Next: N command
*
$ sed '/test/{n; s/aa/bb/} ' example-----if test is matched, move to the next line of the matching row, replace the AA of the line, change to BB, print the line, and then continue.


Variants: Y command
*
$ sed ' 1,10y/abcde/abcde/' example-----convert all ABCDE in 1--10 line to uppercase, note that regular expression metacharacters cannot use this command.


Exit: Q command
*
$ sed ' 10q ' example-----print out line 10th, exit sed.
Keep and get: H command and G command
*
$ Sed-e '/test/h '-e ' $G example-----when the SED processes files, each row is saved in a temporary buffer called the pattern space, and all rows processed will be printed on the screen unless the row is deleted or the output is canceled. The mode space is then emptied and stored in a new line waiting to be processed. In this example, when the row that matches the test is found, it is stored in the mode space, and the H command copies it and stores it in a special buffer called the retention buffer. The second statement means that when the last row is reached, the G command takes out the line that holds the buffer and then puts it back in the pattern space and appends it to the end of the line that already exists in the pattern space. In this case, append to the last line. In simple terms, any row containing test is copied and appended to the end of the file.
Keep and swap: H command and x command
*
$ Sed-e '/test/h '-e '/check/x ' example-----Interchange mode space and keep the contents of the buffer. That is, the line that contains test and check is swapped.


7. Script
The SED script is a list of SED commands that boot the script file name with the-F option when SED starts. Sed is very picky about the commands entered in the script and cannot have any white space or text at the end of the command, separated by semicolons if there are multiple commands on one line. Actions that begin with # comment on rows and cannot span rows.

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.