开发者

replace string rsubmit with * rsubmit

开发者 https://www.devze.com 2023-02-22 07:06 出处:网络
I have text files with lines like these: -------------------------- .... ... rsubmit; ........ ........ endrsubmit;

I have text files with lines like these:

--------------------------  
.... 
... rsubmit;  
........  
........ endrsubmit;  
.......  
...... rsubmit ;  
................  
....... endrsubmit ;  
..........  
-----------------------------  

I want to replace  
all 'rsubmit;'     with '* rsubmi开发者_运维知识库t;'  
all 'rsubmit ;'    with '* rsubmit ;'  
all 'endrsubmit;'  with '* endrsubmit;'  
all 'endrsubmit ;' with '* endrsubmit ;'  

In short, just put star-space at the beginning.

I have tried to use sed 's/rsubmit\;/\* rsubmit\;/g'

but this method cannot take care of those 'endrsubmit'

Can any one help on this ?

Thanks

Alvin SIU


Try this

sed 's/\(end\)\?rsubmit/* &/' 


Ruby(1.9+)

$ ruby -ne 'print $_.gsub(/(.[^ \t]*rsubmit)/,"*\\1")' file
--------------------------
....
...* rsubmit;
........
........* endrsubmit;
.......
......* rsubmit ;
................
.......* endrsubmit ;
..........
-----------------------------

Or awk

$ awk '{for(i=1;i<=NF;i++) { if ($i~/rsubmit/) { $i="* "$i } } }1' file
0

精彩评论

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

关注公众号