sed script that replaces a double quoted macro argument string with just the string

Viewed 39

I want to remove the double quotation marks in macro calls using sed (hundreds of files and thousands of occurrences need to be changed).

for example:

MY_MACRO("any_string");

should be changed to:

MY_MACRO(any_string);

Note that "any_string" is just an example. It could be just any string (the strings will not contain any space characters though).

I've tried:

sed -i '/MY_MACRO(/ s/\"//g' file.c 

but that may remove ":s that shouldn't be removed.

Thanks!

1 Answers

You can use

sed -i '/MY_MACRO(/ s/("\([^"]*\)")/(\1)/g' file.c

Here,

  • /MY_MACRO(/ - matches lines that contain MY_MACRO( substring and then
  • s/("\([^"]*\)")/(\1)/g - searches for (", then zero or more chars other than " (captured into Group 1) and then ") and replaces with ( + Group 1 value + ).

See the online demo.

Related