Saturday, November 19, 2022

[FIXED] How do I extract the string that follows another string from multiple files?

Issue

I have a lot of files in the folder filesToCheck, some examples given below. I need the output result.txt as also shown below. I think the regex CAKE_FROSTING\(\".*\" is needed somehow for this task, but I am not well versed in bash scripting. I can use linux bash with any commands that do not require extra installations.

file1.cpp

something
CAKE_FROSTING("is.simply.the.best", "[no][matter][what]") { DO(something(0) == 1); }

file2.h

something else
CAKE_FROSTING(
"is.kinda.neat", 
"[i][agree]") something else
something more

file3.cpp

hello

file4.cpp

random_text CAKE_FROSTING("Can be nice") "more random text"

CAKE_CREAM("totally.sucks", "[trust][me]")

fileEmpty.h


result.txt

is.simply.the.best
is.kinda.neat
Can be nice

Edit: I tried

awk '"CAKE_FROSTING\("{print $2}' filesToCheck/file1.cpp

but this gives the wrong output "[no][matter][what]") and only runs on one file.


Solution

Using GNU sed

$ sed -Enz 's/.*CAKE_FROSTING\(\n?"([^"]*).*/\1\n/p' /path/to/filesToCheck/*
is.simply.the.best
is.kinda.neat
Can be nice


Answered By - HatLess
Answer Checked By - Cary Denson (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.