2018-1-16 Linux Learning notes [important]

Source: Internet
Author: User

9.4 sed (UP)

Sed is a stream editor, which is a very useful tool in text processing, and grep can only implement a lookup function, and SED can not only find but also replace.
When SED is processed, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file.

Note: When SED is processed, the file content does not change unless you use redirection to store the output.

Sed-n ' 5 ' p test.txt---> Print line 5th
Sed-n ' 1,5 ' p test.txt---> Print 第1-5 line
Sed-n ' 1,$ ' P test.txt---> Print all Lines
Sed-n '/root/' P test.txt---> Print a row containing root
Sed-n '/^1/' P test.txt---> Print all lines beginning with 1
Sed-n '/in$/' P test.txt---> Print all lines in the end
Sed-e ' 1 ' p-e '/222/' p-n test.txt---> Print line 1th and line containing 222
Sed-n '/o\+t/' P test.txt---> Print contains O.. Line of T
Sed-nr '/o+t/' P test.txt---> Print contains O.. The line of T, where the r option is similar to the-e option of grep, plus the ability to avoid the ideographic character
Sed-nr '/root|sshd/' P test.txt---> Print rows containing root or sshd

9.5 sed (bottom)

Sed-n '/bus/' Ip test.txt---> Print rows with bus (case-matched), where i option means case insensitive
Sed ' 1 ' d test.txt---> Delete line 1th (the file itself does not change)
Sed '/root/' d test.txt---> delete the row containing the root (the file itself does not change)
Sed ' 1,3 ' d test.txt---> Delete 第1-3 rows (the file itself does not change)
Sed-i ' 1,3 ' d test.txt---> delete the 第1-3 line of the file ( plus the i option, the file changes )
Sed ' s/[0-9]//g ' test.txt---> Delete all numbers in a document
Sed ' s/[a-za-z]//g ' test.txt---> Delete all letters in a document
Sed ' 1,10s/ot/to/g ' test.txt---> Replace ot in 第1-10 line to
Sed-r ' s/([^:]+):(. *):([^:]+]/\3:\2:\1/' test.txt---> will be replaced with: All rows of the split line at the beginning and end of each line
Sed-r ' s/(. *)/aaa:&/' test.txt---> Add string aaa before all lines:

2018-1-16 Linux Learning notes [important]

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.