Removing new lines in the middle of a function call in the source code

I have source code (in C) that is formatted, and newlines are added in the middle of function calls. for example, I have

CALL_A( par1, par2, 12345); 

and somewhere else I have

 CALL_A(par1, par2 ,12345); 

I need to find the numbers passed as the 3rd parameter of the function. I used this sed command to remove newlines, but it does not match it:

 cat source.c | sed -e ':a; /CALL_A*$/ { N; s/$//; ba; }' 

Any suggestions on how to get rid of a newline in the middle of a function call?

+4
source share
1 answer

Try the following:

 sed -e ':a; /CALL_A[^)]*$/{N; s/\n *//; ba}' 

The current version will not work for the following reasons:

  • /CALL_A*$/ will not match calls, * in regex repeats the previous element, so you are looking for lines ending in CALL_ , and then any number of A I changed this to /CALL_A[^)]*$/ so that it matches lines with CALL_A followed by any number of characters that are not ) , so you will not match lines that have a call on one line.
  • s/$// do nothing, $ matches at the end of a line, but does not match a newline, so replacing it will not be useful. Instead, I used s/\n *// , which will remove the newline and any leading spaces from the next line.
+2
source

Source: https://habr.com/ru/post/1491988/


All Articles