SED command application

Source: Internet
Author: User

SED command:
echo "I am oldboy,my QQ is 1234567" >>oldboy.txt
Requirements:
1. Remove the Oldboy string from the Oldboy.txt file
Method One:
[Email protected] oldboy]# echo "I am oldboy,my QQ is 1234567" >oldboy.txt
[email protected] oldboy]# cat Oldboy.txt
I am oldboy,my QQ is 1234567
[[Email protected] oldboy]# sed ' s#^.*m # #g ' Oldboy.txt
Oldboy,my QQ is 1234567
[[Email protected] oldboy]# sed ' s#^.*m # #g ' oldboy.txt |sed ' s#,.*# #g '
Oldboy
[[Email protected] oldboy]# sed ' s/^.*m//g ' oldboy.txt|sed ' s/,.*//g '
Oldboy
Note: "#" and "/" are both symbols of substitution
Method Two:
[[Email protected] oldboy]# sed ' s#^.*m \ (oldboy\),. *#\1#g ' Oldboy.txt
Oldboy

[[email protected] oldboy]# sed ' s#^.*m \ (old\) \ (boy\),. *#\1#g ' oldboy.txt
Old
[[email protected ] oldboy]# sed ' s#^.*m \ (old\) \ (boy\),. *#\1\2#g ' oldboy.txt
Oldboy
[[email protected] oldboy]# sed ' s#^.*m \ (old\) \ (boy\),. *#\2#g ' oldboy.txt
Boy
\ followed by a number means to take out the contents of the front parenthesis, the first parenthesis represents 1, the second represents 2, and so on.

Requirements:
[email protected] oldboy]# cat Oldboy.txt
I am oldboy,my QQ is 1234567
I am old,but am Strong
Welcome to test
Remove rows containing old without Oldboy in Oldboy.txt
Method One:
[[Email protected] oldboy]# sed '/old[^boy]/d ' oldboy.txt
I am oldboy,my QQ is 1234567
Welcome to test
Method Two:
[[Email protected] oldboy]# sed '/\<old\>/d ' oldboy.txt
I am oldboy,my QQ is 1234567
Welcome to test
"<>" matches a single character
Replace a row with old without a boy with www.baidu.com
[Email protected] oldboy]# sed ' s#old[^boy] #www. Baidu.com#g ' Oldboy.txt
I am oldboy,my QQ is 1234567
I am www.baidu.combut am Strong
Welcome to test
[Email protected] oldboy]# sed ' s#\<old\> #www. Baidu.com#g ' Oldboy.txt
I am oldboy,my QQ is 1234567
I am www.baidu.com,but am Strong
Welcome to test

SED command application

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.