Sed 替换基础
这里介绍了sed
的替换技巧
首先看一下用法:
sed 's/REGEXP/REPLACEMENT/FLAGS' filename
其中:
- s 表示执行替换操作
- 常用的还有 d 表示删除
- / 表示分隔符
- 分隔符还可以用 @ 或者 % 或者 ; 或者 : 表示
- REGEXP 表示匹配的正则
- REPLACEMENT 表示要替换的内容
- FLAGS 表示标志
- g 全局替换, 从头到尾都换
- n 可以是任意数字, 表示的是替换第n次出现的地方
- p 打印两次匹配到的内容, 即,如果替换成功则打印两遍
- i 忽略大小写的匹配
- w 后面跟个文件, 如果产生了替换,则将替换后的结果输出到文件中
然后我们来看这样一个文件:
➤ cat sed.txt
# Instruction Guides
1. Linux Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not
much time available)
#
Additional FAQS
6. Windows- Sysadmin, reboot etc.
1. 把第一个 'Linux' 替换成 'Linux-Unix'
➤ sed 's/Linux/Linux-Unix/' sed.txt
# Instruction Guides
1. Linux-Unix Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux-Unix
5. Productivity (Too many technologies to explore, not
much time available)
#
Additional FAQS
6. Windows- Sysadmin, reboot etc.
这个例子很简单, 不多说了.
2. 把 'Linux' 全部替换成 'Linux-Unix'
➤ sed 's/Linux/Linux-Unix/g' sed.txt #注意,这里是全部替换哦,所以有个g(global)
# Instruction Guides
1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux-Unix
5. Productivity (Too many technologies to explore, not
much time available)
#
Additional FAQS
6. Windows- Sysadmin, reboot etc.
3.仅把第二个 'Linux' 换成 'Linux-Unix'
➤ sed 's/Linux/Linux-Unix/2' sed.txt
# Instruction Guides
1. Linux Sysadmin, Linux-Unix Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not
much time available)
#
Additional FAQS
6. Windows- Sysadmin, reboot etc.
看到了吗, 这里的标志位是2
哦~
4. 全部替换+打印替换内容到屏幕+保存输出到文件
➤ sed -n 's/Linux/Linux-Unix/gpw output' sed.txt
1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
4. Storage in Linux-Unix
------
➤ cat output
1. Linux-Unix Sysadmin, Linux-Unix Scripting etc.
4. Storage in Linux-Unix
这次的例子有点奇怪, 标志位是gpw
我们已经知道g
代表的是全部替换,那么p
和w
又是啥? 通过info sed
我们可以查到:
`p' - If the substitution was made, then print the new pattern space.
`w FILE-NAME' - If the substitution was made, then write out the result to the named file.
现在你懂了嘛? 不懂? 查词典去!
5.仅仅替换特殊的行
➤ sed '/\-/s/\-.*//g' sed.txt
# Instruction Guides
1. Linux Sysadmin, Linux Scripting etc.
2. Databases
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not
much time available)
#
Additional FAQS
6. Windows
看到这个命令与其他的不同了吗?
没错, 就是在s
前面加了点东西, 加的东西就是我们要匹配的行, 这次加的是\-
, 所以就替换了-
后面的内容. 举一反三, 你是不是可以想出别的方案呢? 试试看!
6.删除某些字符
➤ sed 's/\(.....\)\(...\)$/\2/g' sed.txt
# Instructiodes
1. Linux Sysadmin, Linux Scripttc.
2. Databases - Oracle, mytc.
3. Security (Firewall, Network, Online Securtc)
4. Storage nux
5. Productivity (Too many technologies to explnot
much time avle)
#
AdditioAQS
6. Windows- Sysadmin, rebtc.
看懂了吗? 没有? 好, 那看下一个:
➤ sed 's/...$//g' sed.txt
# Instruction Gui
1. Linux Sysadmin, Linux Scripting e
2. Databases - Oracle, mySQL e
3. Security (Firewall, Network, Online Security e
4. Storage in Li
5. Productivity (Too many technologies to explore,
much time availab
#
Additional F
6. Windows- Sysadmin, reboot e
这个应该看懂了吧?
上一个例子是删除了最后三个字符, 上上一个例子是删除了倒数第八到第五个字符, 也就是, 把最后八个字符换成了最后三个字符, 有点绕? 多想一会儿!
再来一个删除html标签的:
➤ echo "This <b> is </b> an <i>example</i>." | sed -e 's/<[^>]*>//g'
This is an example.
最后一个例子:
➤ sed 's/#.*//g' sed.txt
1. Linux Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not
much time available)
Additional FAQS
6. Windows- Sysadmin, reboot etc.
替换显得太难看了, 不如直接删除:
➤ sed '/#.*/d' sed.txt
1. Linux Sysadmin, Linux Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Security (Firewall, Network, Online Security etc)
4. Storage in Linux
5. Productivity (Too many technologies to explore, not
much time available)
Additional FAQS
6. Windows- Sysadmin, reboot etc.
sed
是个好东西, 光info sed
就有98K这么大, 好好看看咯~ (我没看完...逃...