0

I'd like to grep lines starting with printf, however it could not end at that line and it can have more lines with \ (backslash). For instance,

printf("%s %s %s", \
        "one more line", \
        "second one", \
        "third one");

Yes, we can use the option -A (After) but it could include also meaningless lines as it specifies the number how many lines shall be output.

Please let me know if you have a brilliant idea to solve. Thanks.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
windrg00
  • 457
  • 3
  • 9

2 Answers2

2

I don't know a way to do it with grep.

If you allow tools such as perl, there are more options:

perl -ne 'print if /^printf/ .. /;/' input.txt

If a line starting with printf is found, it will be printed along with all following lines until a ; is found.

Or:

perl -ne 'print if /^printf/ .. !/\\$/' input.txt

Same logic, but the stop condition is a line not ending with \.

melpomene
  • 84,125
  • 8
  • 85
  • 148
0
$ cat file
foo
printf("%s %s %s", \
        "one more line", \
        "second one", \
        "third one");
bar
$
$ awk '/printf/{f=1} f; !/\\$/{f=0}' file
printf("%s %s %s", \
        "one more line", \
        "second one", \
        "third one");
$
Ed Morton
  • 188,023
  • 17
  • 78
  • 185