Shell programming The Four musketeers sed

Source: Internet
Author: User
Tags processing text

(1) SED effect

SED is a non-interactive text editor, and the Contrast VI (VIM) is an interactive editor


(2) SED syntax

sed [-option] [command] Files

Comments:

SED tool default processing text, text content output screen has been modified, but the contents of the file is not modified, need to add I parameter to completely modify


(3) SED combat


SED Combat Replacement text

<1> jfedu.txt file, replace all the old in the file with the new

Sed ' s/old/new/g ' jfedu.txt

<2> jfedu.txt file, replace http://www.jd.com with http://www.baidu.com

Sed ' s#http://www.jd.com#http://www.baidu.com#g ' jfedu.txt

SED Actual Combat Print text

<1> jfedu.txt file, print the first line

Sed-n ' 1p ' jfedu.txt

<2> jfedu.txt file, print 1 to 3 lines

Sed-n ' 1,3p ' jfedu.txt


<3> jfedu.txt file, print the last line

Sed-n ' $p ' jfedu.txt

<4> jfedu.txt file, print the first line and the last line

Sed-n ' 1p; $p ' Jfedu.txt


<5> jfedu.txt file, print the first line to the last line

Sed-n ' 1, $p ' Jfedu.txt


<6> jfedu.txt file, print/linux/this line

Sed-n '/linux/p ' jfedu.txt


<7> jfedu.txt file, print the first line and the last line (implemented with the-e extension parameter)

Sed-n ' 1p '-e ' $p ' jfedu.txt

<8> jfedu.txt file, match/linux/this line and/test/this line, print out

Sed-n '/linux/,/test/p ' jfedu.txt


<9> jfedu.txt file, match/linux/this line to the last line, print out

Sed-n '/linux/, $p ' Jfedu.txt


Sed combat Delete text

<1> jfedu.txt file, delete the first line to line 3rd

Sed ' 1,3d ' jfedu.txt


<2> jfedu.txt file, delete the Linux match line to the last line

Sed '/linux/, $d ' Jfedu.txt


Sed actual combat Insert text

<1> jfedu.txt file, find the/linux/line, and add word characters to the next line

Sed '/linux/aword ' jfedu.txt


<2> jfedu.txt file, find the/linux/line, and add word characters on the previous line

Sed '/linux/iword ' jfedu.txt


<3> jfedu.txt file, find/linux/this line, add Word characters on the previous line, add Word characters on the next line

Sed-e '/linux/aword '-e '/linux/iword/' jfedu.txt


<4> jfedu.txt file, find the line that ends with test and add word characters at the end of its line

Sed ' s/test$/&word/g ' jfedu.txt


<5> jfedu.txt file, find the line where/www/, add word characters at the beginning of its line

Sed ' www/s/^/&word/g ' jfedu.txt


<6> jfedu.txt file, look for lines ending in COM and add at the end of the line. Finds the row in which/mpt/is added at the beginning of the line.

Sed-e ' s/com$/&./g '-e '/mpt/s/^/&./g ' jfedu.txt


<7> Print roots with SED

Sed-n ' s/\//p '


<8> sed to read variables and replace them

Website=www.jfedu.net

Sed ' s/www.jd.com/$website/g ' jfedu.net


Shell programming The Four musketeers sed

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.