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