高级Shell脚本中的sed编辑器使用技巧
1. 分支命令与测试命令
在使用 sed 编辑器时,为防止出现问题,分支命令应指定地址模式。若模式不存在,分支将停止。示例如下:
$ echo "This, is, a, test, to, remove, commas." | sed -n '{
> :start
> s/,//1p
> /,/b start
> }'
输出结果:
This is, a, test, to, remove, commas.
This is a, test, to, remove, commas.
This is a test, to, remove, commas.
This is a test to, remove, commas.
This is a test to remove, commas.
This is a test to remove commas.
此例中,分支命令仅在行中有逗号时分支。最后一个逗号被移除后,分支命令不再执行,脚本正常结束。
测试命令 t 与分支命令类似,用于修改 sed 编辑器脚本的流程。不过,它根据替换命令的结果跳转到标签,而非基于地址。若替换命令成功匹配并替换模式,测试命令将分支到指定标签;若未匹配,则不分支。其格式为 [address]t [label] ,若不指定标签,测试成功时 sed
超级会员免费看
订阅专栏 解锁全文
1500

被折叠的 条评论
为什么被折叠?



