当前位置: 动力学知识库 > 问答 > 编程问答 >

regex - Using sed to replace a pattern like given below

问题描述:

I have to change function declaration, so, I need to change all the calling instances.

Existing One: my_func( a, b, c, ***d, e***, f, g);

New One: my_func( a, b, c, **XYZ**, f, g);

Means need to change d, e to XYZ. Here, d and e can represent anything and XYZ is fixed string. So, I need to replace string between 3rd comma and 5th comma with XYZ.

网友答案:

Finally, after lots of trial I found my answer. Below is the command that worked for me.

echo "my_func( a, b, c, d, e, f, g);" | sed 's/\(my_func(\)\(\([^,]*,\)\{3\}\)\([^,]*,[^,]*\)/\1\2 XYZ/g'

Actually, I was working with an instance having pattern with multiple lines.

For multiple line:

my_func( a, b, c, 
         d, e, f, g); 

sed 'N; s/\(my_func(\)\(\([^,]*,\)\{3\}\)\([^,]*,[^,]*\)/\1\2 XYZ/g' file_name > output_file
分享给朋友:
您可能感兴趣的文章:
随机阅读: