简体   繁体   English

匹配sed中的某些行时的多个替代命令

[英]Multiple substitute commands when matching some lines in sed

With Vim's global command, it is possible to chain multiple commands with | 使用Vim的全局命令,可以用|链接多个命令| (pipe) symbol when matching some lines, for example: 匹配某些行时的(管道)符号,例如:

g/match/ s/11/00/ | s/22/11/g

Is this also possible with sed without repeating the match regex? 在不重复match正则表达式的情况下,使用sed也可以吗?

sed -e '/match/ s/11/00/ ; /match/ s/22/11/g ' $file

If not, is it possible to do this with perl? 如果没有,是否可以用perl做到这一点?

You could use: 您可以使用:

echo "->11,22<-
->01,20<-" | sed '/11/ {s/11/00/g; s/22/11/g}'

Output is: 输出为:

->00,11<-
->01,20<-
  • the /11/ restricts the s commands inside its { ... } block only to matching lines /11/将其{ ... }块内的s命令仅限制为匹配的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM