#linux基础 #sed First Contact

Source: Internet
Author: User
Tags line editor

SED basic usage:
Sed:stream Editor Editor
Each time the line editor reads a line of text into memory space, this space is called pattern space. By default, the original file is not edited, only the data in the pattern space is processed, and when finished, the contents of the pattern space are displayed.

sed [OPTION] ... {Script-only-if-no-other-script} [Input-file] ...
-n Silent mode, does not display the contents of the mode space by default
-I directly modify the original file
-e script can execute multiple scripts simultaneously
-f/path/to/sed_script read SED script from file
-R using extended regular expressions

Sed ' addresscommmand ' file ...
ADDRESS:
1.addr1,addr2
2.$
3./regexp/
4./pattern1/,/pattern2/
5.LINENUMBER
6.addr,+n
COMMAND:
D:delete reads the text into the pattern space, deletes the qualifying rows, and displays the remaining content in the pattern space
Sed ' 2,5d '/etc/fstab
P:print reads the text into the pattern space, prints the rows that match the criteria, displays the pattern space and the printed lines, and if you want to display only the printed rows, use the-n option
Sed-n ' 1,4p '/etc/fstab
A \string: Appends a new line of "STRING" after the specified line
I \string: Insert a new line of "STRING" before the specified line
R file: Adds the contents of the specified file to the line following the qualifying row
W File: Writes a line in the specified range of addresses to the specified file
s/pattern/string/modifier: Find substitution, default replaces only the first match to the string in each line.
G: Global Substitution
I: Ignore character case
s///s### [Email protected]@@
\ (\) \1 \2
&: Indicates reference to the previously matched string
Practice:
1. Delete the white space character at the beginning of the history command
History|sed-r ' s/^\s+//'
2. Remove the parent directory of the given path
"/tmp/log/messages/hello" |sed-rn ' s#^/.*/([^/]+)/?#\1#GP '

#linux基础 #sed First Contact

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.