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
  • 172,331
  • 17
  • 70
  • 167
windrg00
  • 437
  • 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
  • 81,915
  • 7
  • 76
  • 137
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
  • 172,331
  • 17
  • 70
  • 167