Use of sed in the shell

Source: Internet
Author: User

1. In the command Line Definition Editor commandcommand format sed options script file
SED does not modify the text file's data, he only sends the modified data to stdout.2. Use multiple editor commands on the command line
Place the command in a file
The 3.S command uses the replacement tagThe number representing the new text will replace the place where the pattern matches the first. g, replace All. P, the original content is printed out. W file, which writes the result of the substitution to the document. The-n command disables print mode space, with-N and p to print only the substituted rows.
4. Use Address 4.1 to specify line sed ' 2s/dog/cat/' data1sed ' 2,3s/dog/cat/' data1sed ' 2, $s/dog/cat/' data14.2 match text-mode sed '/farsight/s/bash/ csh/'/etc/passwd4.3 Combo command
5. Delete RowsSed ' d ' data1 all remove sed ' 3d ' data1 delete the third line sed ' 2,3d ' data1 delete 2-3 lines sed '/quick/d ' data1 delete match to Quick line between two modes deleted. Sed '/1/,/3/d ' data1 delete rows matching to 1 to 36. Inserting and attaching textSed ' 3i\>this is a inserted line. ' Data1 insert 3 lines in front of sed ' 3a\>this is a inserted line. ' Data1 put on 3rd row after sed ' $a \>this is a Inserted line. ' Data1 put multiple lines behind the last line
7. Modify the rowSed ' 3c\>this is a changed line. ' Data1 changes the 3rd row to sed '/number1/c\>this is a changed line. ' Data1 modify the matching row sed ' 2,3c\>t He is a changed line. ' Data1 Modify lines 2 to 38. Print PSed-n ' 2,3p ' data1 print 2 to 3 lines sed-n '/3/{>p>s/line/test/p>} ' data1Prints the third line, and matches the line after the replacement row. Print line number sed ' = ' data1
Print line numbers for matching lines
9. Work with the file write file sed ' 1,2w test ' data1 data1 1 to 2 lines write test read data from a file put the data in data3 after the 2nd line of Data2
Sed ' $r data3 ' data2Place data from Data3 on the last side of Data2Sed '/line1/r data3 ' data2 read data3 placed in data2 after matching line1The moment of dazzling skill:write the contents of the file data3 after matching to tag, and then delete the tag
------------------------------------------------SED Advanced1.N: reads the next line into the pattern space N: Attaches the next line to the mode space p:print up to the first embedded newline of the "the current". D:delete up to the first embedded newline in the pattern space. Start next cycle, but skip reading from the input ifthere is still data in the pattern space.
2. Mode space and hold space
H Copy pattern space to hold space
h append to hold space
g will keep space Copy to mode space
g will keep space append to mode space
X swap mode space and the content of the hold space
Flips a file, using pattern space and holding space to flip the file.
Exclude Commands! : Print rows that do not match
3. Change the FlowJump Commandb [lable] jump to the end of the script without lable, add lable to jump to the corresponding lable. You can also jump to the previous tabTest
t [lable] The current command is set to jump.4. Mode substitution4.1and (&) symbol& represents what was previously matched.
4.2 Replacing a single wordwhen matching to name is, only the content that name,\1 represents in the first parenthesis is extracted.
5. Using sed in ScriptsEncapsulates the SED command in a script.
Redirect the output of sed, using ' ' to assign the output of SED to the variable.6. Create an SED utility script6.1 Doubling line spacingTake advantage of the preserve space default value.
To double the line spacing with multiple blank lines, the idea is to first delete all blank lines and add blank lines.
6.2 to the line number in the file
6.3 Print the end lineUse the sliding window to display the last few lines. $q will exit at the end, N read the next line to the pattern space, one by one, $D from line 11th to the last row to delete the first row of the pattern space. So the window size is 10 rows.
6.4 To delete contiguous blank lines
Creates an interval that begins with any character and ends at the interval of a blank line.6.5 Delete blank lines at the beginning
Creates an interval of any character to the end line.6.6 Delete The end of the lineCollects all blank rows at the end of the delete.


Use of sed in the shell

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.