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

Time:11-06

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?


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

CodePudding user response:

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
  • Related