0

I need to grep over all files in a directory for the function AbcXyz( and dump the outputs to a single file. But there are 2 caveats :

  1. I need to redirect grep outputs to separate lines, i.e. each instance of AbcXyz(...) identified has to appear in a separate line.

  2. The function takes many parameters and might not be present in one line

In Code :

   AbcXyz(param1, param2, "Some message .... 
          .... Some more message", param3)

Grepped Output :

AbcXyz(param1, param2, "Some message ........ Some more message", param3)

How can I do this ?

2 Answers2

1

Regex (grep) for multi-line search needed Might help.

In short:

  1. Try awk awk '/Abc/,/\)/' your_file - will find and print the pattern from 'Abc' to ')' https://www.gnu.org/software/gawk/manual/html_node/Ranges.html (7.1.3).

  2. grep + regex (See link)

Y.R.
  • 371
  • 1
  • 7
1

It can't be done robustly without writing a parser for whatever language your input file contains but with GNU awk for multi-char RS and assuming your input always looks as you show, etc.:

awk -v RS='AbcXyz[(][^)]*)' 'RT{$0=RT; $1=$1; print}' file

e.g.:

$ cat file
   AbcXyz(param1, param2, "Some message ....
          .... Some more message", param3)
   AbcXyz(param1, param2,
                 "Some message ....
          .... Some more message",
 param3)
   AbcXyz(param1, param2, "Some message .... .... Some more message", param3)

$ awk -v RS='AbcXyz[(][^)]*)' 'RT{$0=RT; $1=$1; print}' file
AbcXyz(param1, param2, "Some message .... .... Some more message", param3)
AbcXyz(param1, param2, "Some message .... .... Some more message", param3)
AbcXyz(param1, param2, "Some message .... .... Some more message", param3)
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • The problem here is that some of the parameters could be function calls themselves, like param2 = sizeOf(int_32), etc. So in that case there will be nested brackets within the outer () brackets. Thats why matching this has become really difficult for me. Any other quicker way? @EdMorton – Nashez Zubair Jul 05 '21 at 20:17
  • As I said `It can't be done robustly without writing a parser for whatever language your input file contains`. The script I posted generates the output you asked for from the input you provided, that's all we can do using a text processing script. If the example in your question isn't adequate to demonstrate your problem then edit your question to fix it but idk if you're going to get a solution if the inputs much more complicated than already shown. – Ed Morton Jul 05 '21 at 20:33