One of the powerful tools under Linux Sed,shell Essentials

Source: Internet
Author: User
Tags control characters

SED command basic usage
SED is a non-interactive text editor that edits text files and standard input, which can be input from keyboard input, file redirection, strings, variables, text from pipelines, and so on.
Sed reads data from one line of text or standard input, copies it to a buffer, and then reads the command line or script's first command, edits the line number required by this command, and repeats the process until all the commands in the command line or script are executed. SED can process all editing commands at once, very efficient

SED is suitable for the following three scenarios:
* Editing is too large for an interactive text editor
* Editing commands are too complex to be entered in an interactive text editor
* Scan the file once, but need to perform multiple editing functions

Sed simply edits a copy of the original file in the buffer and does not edit the original file. Therefore, if you need to save the changes and you need to redirect the output to another file, you can use the following command:
sed ' sed command ' input_file > Result_file
Or another way is the-w option, which is followed by

There are three ways of calling Sed:
① called directly on the shell command line
# sed [options] ' sed command ' input file
② invokes the script with the SED command after inserting the SED command into the script file:
# sed [options]-F sed script file name input file
③ after the SED command is written to the script file, it +x into an executable
./sed script File Input file
The third way is to add #!/bin/sed to the head of the file

Common options for SED commands:
-N Do not print all rows to standard output
-e means that the next string is resolved to the SED edit command, and if only one edit command is passed,-e can omit
-F indicates that the SED script file is being invoked

Sed commands are usually composed of positioned text lines and sed edit commands, and the SED edit command performs various editing processes on the row to which it is located
SED provides two ways to position lines of text:
* Use line numbers to specify a row or line number range
* Use regular expressions

How the SED command locates text lines:

Options Significance
X X is the specified line number
X, y Specify range of line numbers from X to Y
/pattern/ Querying rows that contain patterns
/pattern/pattern/ Querying rows that contain two patterns
/pattern/,x Line from match pattern to line X
x,/pattern/ Line from line x to matching line of pattern
x,y! Querying rows that do not contain x and Y line numbers



=================================== Split Line =======================
SED Edit Command table

Options Significance
P Print matching lines
= Print matching line numbers
A\ Append text information after locating a row
I\ Inserting text information before locating a row
D Delete Anchor row
C\ Replace anchor row with new text
S Replace the corresponding pattern with the replacement mode
R Read text from another file
W Write text to another file
Y Transform character
Q Exit when the first pattern match is complete
L Displays the control characters equivalent to the octal ASCII code
{} Command Group to execute on the anchor row
N Reads the next input line and processes the new row with the next command
H Copy the text of the pattern buffer to the hold buffer
H Append the text of the mode buffer to the hold buffer
X Swap mode buffers and keep buffers in content
G Copy the contents of the hold buffer to the mode buffer
G Append the contents of the hold buffer to the mode buffer



======================== Split Line ============================
Detailed options and editing commands:
The-n option of the 1,sed command and the P command
# sed-n ' 1p ' input
# sed ' 1p ' input
As can be seen from the output, add-n after the standard output only the first line printed out, and not add-n time, first printed line, and then print the entire file content. So the meaning of the-n option is to not print the entire contents of the SED edit, which is input. To print only matching rows
# sed-n ' 3,6p ' input--print from 3 to 6 lines
# sed-n '/certificate/p ' input--print match pattern line, note case is sensitive

The-e option of the 2,sed command
Because SED does not support the use of multiple editing commands at the same time, you need to specify each edit command with the-e option
# sed-n-E '/certificate/p '-e '/certificate/= ' input

The-f option for the 3,sed command
The-F option only works when the SED script file is invoked, and features such as appending text, inserting text, modifying text, deleting text, and replacing text often require several SED commands to complete, so these commands are often written to the SED script, and then the SED script is called to complete.
Here is the script for this sed:
SED code 1. #!/bin/sed-f
2./file:/a\
3. We Append a new line.\
4. We append another line.
Note: \ In the above/file:/a\ means adding content after a new line is changed, and \ In the following content also represents a newline.

A set of examples of 4.2.2 sed text positioning
1. Match meta-characters
If the target string contains metacharacters, you need to use the escape character \ Mask its special meaning.
# sed-n '/\./p ' input--prints the line containing the.
2. Using metacharacters to match
SED can be flexibly used to match the metacharacters of regular expressions, but note that $ in the regular expression represents the end of the line, but in the SED command it represents the last line, and the middle of the write in//is the end of the line, haha.
# sed-n ' $p ' input--print last line
# sed-n '/^$/p ' input, print blank line
3.! symbols
It means to reverse, that is, when the condition is not satisfied.
# sed-n '/.*this/!p ' input--print lines that do not contain this
# sed-n ' 3,6!p ' input--print lines that are not between 3 and 6

A set of examples of basic editing commands for 4.2.3 SED
1. Inserting text
Inserting text is similar to appending text, except that the append text is inserted after the matching line, and the inserted text is inserted before the matching line.
The SED inserts the text symbol bit i\, and the text is inserted in the following format:
Sed ' Specify address i\text ' input file
Create a new script named Insert.sed, which reads as follows:
Bash Code 1. #!/bin/sed-f
2. # is comment
3. # date:2013/06/02
4. # Author:xiongneng
5./this is/i\
6. We Insert a new line #插入的文本内容

2. Modify the text
Modified text refers to the matching of the line with the new text substitution, that is, only the entire row of substitution, sed modified text symbol bit c\
Sed ' Specify line c\text ' input file

3. Delete text
Sed delete text command to delete a specified line or a specified line range, and the delete text symbol for SED is D
Sed ' Specify line d ' input file

4. Replace text
The SED replacement text replaces the string found in the matched line of text with a new string, which is commonly used. While the above modified text can only be full line, SED replaces the text symbol as S
Sed-n ' s/replaced string/new string/P '--only replaces first found in each line
Sed-n ' s/replaced string/new string/GP '--replace all found in each row
Sed-n ' s/replaced string/new string/NP '--replace the nth found in each line
There is an important symbol &amp in the SED replacement text, which represents saving the substituted string for invocation.
For example, the following two command levels: (all the This string is enclosed in parentheses)
# sed-n ' s/this/(&)/GP ' input
# sed-n ' s/this/(this)/GP ' input

5. Write to a new file
All mentioned above is to edit the contents of the input files in the buffer, if you want to save the editing results, you need to redirect the edited text to another file, sed writes the file symbol W, the basic format is:
# sed-n ' 1,5 w output ' input
# sed-n '/this/2 output ' input
# sed-n ' s/this/(&)/GW output ' input--with parentheses, the matching line is written to output
Note: This writes to the new file only the matching rows of the modified data, if you want the entire file content to be written in, using redirect >> or >

6. Read text content from a file
The SED command can also read the contents of other files and append them to the specified address, and the symbol for the SED read-in file is R
# sed '/this/r otherfile ' input

7. Exit command
The Q option of the SED command indicates the exit immediately after the specified address match has been completed
# sed-n '/this/p ' input--print out all matching strings
# sed-n-E '/this/p '-e '/this/q ' test.txt--immediately after finding the first matching print

8. Transform commands
The y option of the sed command represents a character transformation that transforms a series of characters into corresponding characters, one by one.
# sed ' y/transformed character sequence/transformed character sequence/' input file
# sed ' y/12345/abcde ' input--turns input 1 into a,2 into b,3 to c,4 to d,5 to E

9. Display control characters
The SED 1 command can display the control characters in the file, such as backspace, F1, SHIFT, etc.
# sed-n ' 1,$1 ' input

10. Executing a command group in the anchor row
The {} in the sed edit command can specify the set of commands that are executed on the anchor row, similar to the-e option of SED, in order to perform multiple editing commands on the anchor line.
The following command is equivalent:
# sed-n-E '/this/p '-e '/this/q ' test.txt
# sed-n '/this/{p;q} ' test.txt--obviously this more NB Dot

A set of examples of 4.2.4 SED advanced editing commands
1. Processing the next line of matching rows
The meaning of the sed edit command n is to read the next input line and process the row with a command after N, since there is usually more than one edit command, so the command n needs to be used with {}
# sed '/this is/{n;s/her/him/;} ' input--find the line that is the keyword and replace her next line with her him

2. Processing of SED buffers
All of the editing commands mentioned above are copies of the input files to the buffer, processing the contents of the cache. In fact, SED has two buffers, mode buffer pattern buffer and hold buffer. The front is the pattern buffer, while the hold buffer is another memory space, some of the SED's editing commands can be processed to keep the buffer, and swap with the contents of the schema buffer
# Use of sed h, x, G commands
# sed '/subject/h; /object/x; $G ' input
Explained below:
The first h command is to copy the schema buffer contents to the hold buffer, that is, when the row of subject is found, it is copied to the holding buffer, if the line has an object keyword to swap the mode buffer and keep the cache content after the output mode buffer content, The third command indicates that if the last line is reached, the content of the hold buffer will be output directly.

H and H, G and G are two sets of corresponding commands
The h and H commands are to replace the schema buffer content with the content of the hold buffer, but H is the copy that will be kept in the buffer, and the H is appended, that is, adding new content to keep the buffer content.
G and G are the contents of the buffer that will keep the buffer contents replaced, and likewise, G is the copy and G is append.

3. Use semicolons to split multiple editing commands, which is a bit more NB than the-e option. ^_^
# sed '/subject/h; /object/x; $G ' input

One of the powerful tools under Linux sed (go), Shell essentials

Related Article

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.