Page 1 of 1

Extract Text String Between Curly Braces From Multiple Files in a Directory And Save Extracted to File

Posted: 12 Jun 2017 09:06
by bearwires
I am hoping someone can help me with this issue.

I have a folder with multiple text files.
Each text files contains piped words/text between curly braces like this.....

{a|b|c|d} ....... {e|f|g|h} ........ {i|j|k|l|m} .....etc

OR sometimes nested curly braces like this....

{{a|b|c|{d|d1|d2|d3}}|{e|{f|f1|f2|f3}|g|h}|{i|j|k|{l|l1|l2|l3}|m}} ........... {{n|o|p|q}|{r|s|t|u}|{v|w|x|y|z}}

I need to extract all text strings between every {} and output to a single text file, each text string on a separate line.
The output needs to be in this format.....

a|b|c|d
d|d1|d2|d3
e|f|g|h
f|f1|f2|f3
i|j|k|l|m
l|l1|l2|l3
n|o|p|q
r|s|t|u
v|w|x|y|z

Any help to achieve this goal would be much appreciated.

Re: Extract Text String Between Curly Braces From Multiple Files in a Directory And Save Extracted to File

Posted: 12 Jun 2017 10:41
by aGerman
You could use JREPL.BAT.
Try that code

Code: Select all

@echo off &setlocal
(for %%i in (*.txt) do @for /f "delims={}" %%j in ('jrepl.bat "{[^{}]+}" "" /F "%%~i" /MATCH') do @echo %%j)|sort
pause

Steffen

Re: Extract Text String Between Curly Braces From Multiple Files in a Directory And Save Extracted to File

Posted: 12 Jun 2017 16:04
by igor_andreev

Code: Select all

sed -r -e "s/(\x7b[^\x7b\x7d]{1,}\x7d)/\n\1\n/g" *.txt | sed -n "/^\x7b.*\x7d$/p" | sed "s/^\x7b//;s/\x7d$//" | sort>new.txt

Re: Extract Text String Between Curly Braces From Multiple Files in a Directory And Save Extracted to File

Posted: 22 Jun 2017 22:19
by PaperTronics
As aGerman suggested using JREPL.bat is the solution. I suggest that you learn how to use JREPL as it will come handy in many cases when FINDSTR doesn't seem to work.