sed :替换字符“A”放在字符串的一部分中并将其保留在其他部分中
一切都在标题中:
这是多行文件的字符串模式:
foo_bar_alpha = "a1b2c3_cat_andthis"
barfoo_bar_alpha = "just a int number"
loremfoo_bar_beta = "192.168.0.0"
... other lines come here ...
使用 sed (和/或 awk 和/或 perl),我需要用“.”替换字符“_”。 ,但仅限于字符串的第一部分,即由“=”分隔的右侧,并保留第二部分(“=”之后)。
诸如此类:
sed "s/(.*)=(.*)/ \1 with "_" replaced by "." = \2/g" < my_file
¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
how to do that ?
谢谢。
All is in the title :
here is the string pattern of a multi-line file:
foo_bar_alpha = "a1b2c3_cat_andthis"
barfoo_bar_alpha = "just a int number"
loremfoo_bar_beta = "192.168.0.0"
... other lines come here ...
Using sed (and/or awk and/or perl), I need to substitute char "_" with "." , but only in the first part of the string, which is right delimited by "=", and keep the 2nd part (after "=").
Sthg like :
sed "s/(.*)=(.*)/ \1 with "_" replaced by "." = \2/g" < my_file
¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
how to do that ?
Thx in adv.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
输入
输出
*注意:如果您确实需要在
=
上进行分隔,因为您的变量名称以某种方式包含空格(非常可疑),那么这是有效的:Input
Output
*Note: If you for sure need to delimit on
=
because your variable names somehow contain spaces (highly doubtful) then this works:粗略但有效:
Crude but effective:
使用 awk 代替 sed
use awk instead of sed