Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I extract specific strings from multiple files and write them to .txt in bash?

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 can use linux bash with any commands that do not require extra installations.

The try-out below (with help from stackoverflow) has two problems when I execute it. It only looks for one instance of CAKE_FROSTING despite the global flag g and the file result.txt remains empty despite > result.txt.

sed -Enz 's/.*CAKE_FROSTING\(\n?"([^"]*).*/\1\n/gp' filesToCheck/* > result.txt

What do I need to change?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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

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

CAKE_CREAM("totally.sucks", "[trust][me]")
random_text CAKE_FROSTING("Can be very nice") "indeed"

desiredResult.txt

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

currentResult command line output:

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

>Solution :

Assuming the string CAKE_FROSTING occurs once per line, you can try this sed

$ sed -En ':a;N;s/.*CAKE_FROSTING\(\n?"([^"]*).*/\1/p;ba' filesToCheck/*
is.simply.the.best
is.kinda.neat
Can be nice
Can be very nice
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading