Use of SED (1)-S parameter/replace

Source: Internet
Author: User

1. Sed syntax command

[Address] command or

Address {command

Command

Command

}

If no address is specified, use the command to match each line.

1 ABCD

TS 2 ABCD

3 ABCD

PS 10

Vs 12

Te 4 ABCD

 

6 ABCD

7 ABCD

CD --- AB

CD -- AB

See section 1.4

See section 12.44

Class: Group

Contrator: Employee

 

Example:

Sed-n'/^ TS/S/ABCD/efgh/P' Test

=> TS 2 efgh ABCD

 

Sed-n'/^ TS/S/ABCD/efgh/2P 'test # Add a number to specify the nth match of the row

=> TS 2 ABCD efgh

 

Sed-n'/^ TS/S/ABCD/efgh/PG 'test # full match of the row

=> TS 2 efgh

 

Sed-n'/^ TS /! S/ABCD/efgh/PG 'test # address added after! Apply to all rows that do not match the row

=>

1 efgh

3 efgh

Te 4 efgh

6 efgh

7 efgh

 

Sed-n'/^ TS/,/^ te/S/ABCD/efgh/PG 'test # match rows starting with TS to rows starting with Te

=>

TS 2 efgh

3 efgh

Te 4 efgh

 

Sed-n'/^ TS/,/^ te/S/ABCD/efgh/wha.txt 'test returns the result of sed to the file ha.txt

Cat ha.txt

=>

TS 2 efgh

3 efgh

Te 4 efgh

 

2. Sed grouping command

Sed '/^ TS/,/^ te/{S/ABCD/efgh/;/^ PS/d}' Test

 

3. & indicates the content of pattern in replacement

Sed's/See section [0-9] [1-9] */. [0-9] [1-9] */(&)/'test

=>

(See section 1.4)

(See section 12.44)

 

4. In SED, the escaped brackets enclose any part of the regular expression (pattern) and can be called back in the replacement part.

Sed's // (. */):/(. */) // 2:/1/'test

=>

GROUP: Class

Employee: contrator

 

5. If the replacement part contains special symbols, such as/, you can use another symbol to replace/In sed/

Sed should be included in ''When referencing external variables.

Item = Kernel

Value =/home/yazi/Default

 

Sed-n'/^ '$ item'/P' $ Profile
Kernel = <kernel path>

Now we need to replace the second half of = with the value of value.

 

Sed-n'/^ '$ item'/S &/(. */) =. * &/1 =' $ value' & P' $ Profile

 

 

 

 

 

 

 

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.