Issue
I need to extract the string between CAKE_FROSTING("
and ",
. If the string extends over multiple lines, the quotation marks and newline at the line changes must be removed. I have a command (thanks stackoverflow) that does something in that direction, but not exactly. How can I fix it (and can you shortly explain the fixes)? I am using Linux bash.
sed -En ':a;N;s/.*CAKE_FROSTING\(\n?\s*?"([^,]*).*/\1/p;ba' filesToCheck/* > result.txt
filesToCheck/file.h
something
CAKE_FROSTING(
"is supreme",
"[i][agree]") something else
something more
something else
CAKE_FROSTING(
"is."kinda" neat"
"in fact",
"[i][agree]") something else
something more
result.txt current
is supreme"
is."kinda" neat"
result.txt desired
is supreme
is."kinda" neat in fact
Edit: With help from @D_action I now have
sed -En ':a;N;s/.*CAKE_FROSTING\(\n?\s*?"([^,]*).*,/\1/p;ba' filesToCheck/* > result.txt
this produces almost the correct output, but there are unnecessary quotation marks and one too many newline in the output:
result.txt current
is supreme"
is."kinda" neat"
"in fact"
Solution
Using GNU sed
$ sed -En ':a;N;s/.*CAKE_FROSTING\(\n?\s"([^"]*[^\n,]*)["].*\n"([[:alpha:] ]+)?.*/\1 \2/p;ba' input_file
is supreme
is."kinda" neat in fact
Answered By - HatLess Answer Checked By - Robin (WPSolving Admin)