Linux Text Processing command "sed" simple solution

Source: Internet
Author: User

Text Processing Edit command SED


Command format:

sed [option] ' (intercepted line) [action] ' file name


Option:-N: Only the rows that have been processed by SED are output

-e: Allow more than one action to be entered

-i:sed the modified result to the file

Intercept line: (1) Enter line number directly

(2)/Regular expression/

(3) x, y lines, or a regular replacement

(4) x,y! Reverse

(5) X~y starting from X line, step y line

(6) $ document at the end

Action: P print eg:sed-n ' (line) p ' student.txt

Add a new line after line A, add a new line before the I line

Sed ' 1,5a ============= ' student.txt

Sed '/regular//============ ' Student.txt

D Delete a row

Sed '/regular/d ' student.txt

Sed ' 4d ' student.txt

C replaces the specified line

Sed ' 2c canglaoshi bujigee ' Student.txt

Sed '/regular/C Lalalalalalala ' Student.txt

s replaces the specified string (replaces the first one in each line), and the end G indicates that each line

Sed '/Regular (anchor line)/s/regular (old)/ABCD (new)/' Student.txt

Sed ' s/regular (old)/ABCD (new)/' Student.txt (full text replacement)

Sed '/www/s/33/000/g ' zz_test.txt


Operation: {} Multiple command combinations, separated by;

Sed ' {1,20p;s/www/s/33/000//g} '

& Take the original character

Sed ' s/abc/&def ' student.txt (change ABC to ABCDEF)

U string first letter to uppercase

Sed ' s/abc/\u&/' student.txt (change ABC to ABC)

L Change the first letter of the string to lowercase

U string All letters are changed to uppercase

Sed ' s/abc/\u&/' student.txt (change ABC to ABC)

L string All letters are changed to lowercase

R inserts a file into the specified line of another file

Sed ' 1r abc.txt ' 123.txt (inserts the contents of Abc.txt into line 1th of the 123.txt file)

Q Exit SED

Sed '/false/q ' student.txt (Find a false to exit SED)


This article from "Sevenot" blog, reproduced please contact the author!

Linux Text Processing command "sed" simple solution

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.