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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bearwires
Posts: 7
Joined: 06 Jun 2017 06:40

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

#1 Post by bearwires » 12 Jun 2017 09:06

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 12 Jun 2017 10:41

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

igor_andreev
Posts: 16
Joined: 25 Feb 2017 12:55
Location: Russia

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

#3 Post by igor_andreev » 12 Jun 2017 16:04

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

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

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

#4 Post by PaperTronics » 22 Jun 2017 22:19

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.

Post Reply