sed 的使用(一) -- s参数/替换
2013-05-22 16:02
127 查看
1. sed的语法命令
[address] command 或者
address {command
command
command
}
如果没有指定地址,将命令运用于匹配的每一行
1abcd
TS 2abcd abcd
3abcd
ps 10
vs 12
TE 4abcd
6abcd
7abcd
cd---ab
cd--ab
See Section 1.4
See Section 12.44
Class:Group
Contrator:Employee
例:
sed -n '/^TS/ s/abcd/efgh/p' test
=> TS 2efgh abcd
sed -n '/^TS/ s/abcd/efgh/2p' test #加数字可以指定本行第n次匹配
=> TS 2abcd efgh
sed -n '/^TS/ s/abcd/efgh/pg' test #本行全匹配
=> TS 2efgh efgh
sed -n '/^TS/! s/abcd/efgh/pg' test #地址后加!应用于不匹配该行的所有行
=>
1efgh
3efgh
TE 4efgh
6efgh
7efgh
sed -n '/^TS/, /^TE/ s/abcd/efgh/pg' test #匹配从以TS开头的行到以TE开头的行
=>
TS 2efgh efgh
3efgh
TE 4efgh
sed -n '/^TS/, /^TE/ s/abcd/efgh/wha.txt' test #将sed的结果重定向到文件ha.txt
cat ha.txt
=>
TS 2efgh efgh
3efgh
TE 4efgh
2. sed的分组命令
sed '/^TS/,/^TE/ {s/abcd/efgh/;/^ps/d}' test
3. & 在replacement中, 表示pattern的内容
sed 's/See Section [0-9][1-9]*/.[0-9][1-9]*/(&)/' test
=>
(See Section 1.4)
(See Section 12.44)
4. 在sed中,转义的括号括住正则表达式的任意部分(pattern),可以在replacement部分中回调
sed 's/\(.*\):\(.*\)/\2:/1/' test
=>
Group:Class->Class:Group
Employee:Contrator->Contrator:Employee
5. 如果替换的部分包括特殊符号,如/,可以用另外的符号来替代sed中的/
sed引用外部变量时,要用''括起来。
item=KERNEL
value=/home/yazi/default
sed -n '/^'$item'/p' $profile
KERNEL=<kernel path>
现在要把=的后半部分替换成value的值
sed -n '/^'$item'/s&/(.*/)=.*&/1='$value'&p' $profile
[address] command 或者
address {command
command
command
}
如果没有指定地址,将命令运用于匹配的每一行
1abcd
TS 2abcd abcd
3abcd
ps 10
vs 12
TE 4abcd
6abcd
7abcd
cd---ab
cd--ab
See Section 1.4
See Section 12.44
Class:Group
Contrator:Employee
例:
sed -n '/^TS/ s/abcd/efgh/p' test
=> TS 2efgh abcd
sed -n '/^TS/ s/abcd/efgh/2p' test #加数字可以指定本行第n次匹配
=> TS 2abcd efgh
sed -n '/^TS/ s/abcd/efgh/pg' test #本行全匹配
=> TS 2efgh efgh
sed -n '/^TS/! s/abcd/efgh/pg' test #地址后加!应用于不匹配该行的所有行
=>
1efgh
3efgh
TE 4efgh
6efgh
7efgh
sed -n '/^TS/, /^TE/ s/abcd/efgh/pg' test #匹配从以TS开头的行到以TE开头的行
=>
TS 2efgh efgh
3efgh
TE 4efgh
sed -n '/^TS/, /^TE/ s/abcd/efgh/wha.txt' test #将sed的结果重定向到文件ha.txt
cat ha.txt
=>
TS 2efgh efgh
3efgh
TE 4efgh
2. sed的分组命令
sed '/^TS/,/^TE/ {s/abcd/efgh/;/^ps/d}' test
3. & 在replacement中, 表示pattern的内容
sed 's/See Section [0-9][1-9]*/.[0-9][1-9]*/(&)/' test
=>
(See Section 1.4)
(See Section 12.44)
4. 在sed中,转义的括号括住正则表达式的任意部分(pattern),可以在replacement部分中回调
sed 's/\(.*\):\(.*\)/\2:/1/' test
=>
Group:Class->Class:Group
Employee:Contrator->Contrator:Employee
5. 如果替换的部分包括特殊符号,如/,可以用另外的符号来替代sed中的/
sed引用外部变量时,要用''括起来。
item=KERNEL
value=/home/yazi/default
sed -n '/^'$item'/p' $profile
KERNEL=<kernel path>
现在要把=的后半部分替换成value的值
sed -n '/^'$item'/s&/(.*/)=.*&/1='$value'&p' $profile
相关文章推荐
- sed 的使用(一) -- s参数/替换
- 使用sed进行文字替换
- Linux中使用sed命令替换字符串小结
- 使用sed替换文本
- linux中sed命令替换包含引号、斜杠等特殊字符的的使用
- 使用sed替换一个字符为N个字符
- Linux中使用sed命令替换字符串
- sed替换命令的使用
- 使用sed替换连续多行
- sed当中使用变量替换以及执行外部命令
- 【实用手记】使用sed命令替换指定文件内容
- 使用shell命令sed将指定目录下的所有文件中指定的字符串替换成指定的字符串
- Linux中使用sed命令替换字符串小结(转)
- 在shell中使用sed命令替换/为\/
- Linux中使用sed命令替换字符串小结
- sed使用——替换
- 使用sed做符号替换
- [Sed] 使用替换结果作为命令执行
- sed替换字符串时,使用正则表达式的注意事项
- mac下 使用sed 将分号替换为回车