开发者

sed : Sustitute char "A" in a portion of a string and keep it in other portions

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开发者_如何学Go 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.


 awk '{gsub("_",".",$1)}1' ./infile

Input

$ cat ./infile
foo_bar_alpha = "a1b2c3_cat_andthis"
barfoo_bar_alpha = "just a int number"
loremfoo_bar_beta = "192.168.0.0"

Output

$ awk '{gsub("_",".",$1)}1' ./infile
foo.bar.alpha = "a1b2c3_cat_andthis"
barfoo.bar.alpha = "just a int number"
loremfoo.bar.beta = "192.168.0.0"

*Note: If you for sure need to delimit on = because your variable names somehow contain spaces (highly doubtful) then this works:

awk -F= '{gsub("_",".",$1)}1' OFS="=" ./infile


Crude but effective:

sed 'h;s/.*=/=/;x;s/=.*//;s/_/\./g;G;s/\n//' filename


use awk instead of sed

awk 'BEGIN{OFS=FS="="}{gsub("_",".",$1)}1' file

ruby -F"=" -ane '$F[0].gsub!(/_/,".");print $F.join("=")' file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜