替代标志中的一个或多个"g" [英] more than one number or 'g' in substitute flags

查看:135
本文介绍了替代标志中的一个或多个"g"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac OS中使用sed时,不能使用如下替代标志:

when I use sed in mac OS, I can't use substitute flags like this:

sed 's/xx/yy/3g' my.txt 

该命令的替代品是什么? 我不能在这里一起使用"3g".该用例在参考文档中.

what's the replacement for this command? I cannot use "3g" together here. This use case is in the reference documentation.

我使用BSD sed.它显示错误:

I use BSD sed. It show error:

more than one number or 'g' in substitute flags

推荐答案

这可能对您有用(GNU sed):

This might work for you (GNU sed):

sed 's/x/\ny/3;h;s/x/y/g;H;g;s/\n.*\n//' file

在要保留的事件之后放置一个唯一标记,将行复制到保留空间(HS),替换所有事件,然后将这些事件附加到副本中,并删除两个唯一标记之间的块.

Place a unique marker after the occurences you want to preserve, copy the line to the hold space (HS), replace all occurences then append these to the copy and remove the chunk between the two unique markers.

大多数sed都应该接受此方法.如果您使用的sed无法处理\n,请使用唯一的字符串,例如@@a unique string @@

This method should be accomodated by most sed's. If the sed your using does not handle \n's then use a unique string e.g. @@a unique string @@

这篇关于替代标志中的一个或多个"g"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆