I am about to write a batch-file to get lines from a textfile and write only the content between two "" (double quotes) to another textfile.
e.q. fileinput:
WRITE 1,48,1,"1> MODUL 2 TYPENKONTROLLE "
WRITE 1,56,1,"2> MODUL 6 PRAEGETIEFE "
Some other text...
WRITE 1,64,1,"__________________________"
fileoutput:
"1> MODUL 2 TYPECONTROLE "
"2> MODUL 6 PRAEGETIEFE "
"__________________________"
my not working batch:
@echo File:
set /p file=
FOR /F delims^=^" %%i in ('findstr -i -r -c:"[\"]^" %file%.txt') do (
echo %%i >> %file%strings.txt
)
I think i need something like this:
@echo File:
set /p file=
FOR /F delims^=^" tokens^=1,2 %%i in ('findstr -i -r -c:"[\"]^" %file%.txt') do (
echo %%i not needed!
echo %%j >> %file%strings.txt
)
Can someone help me with my problem?
If you are looking for a pure batch solution, then this is probably all you need. It uses nasty looking escape sequences in the FOR /F options to allow specification of "
as your token delimiter.
@echo off
>"output.txt" (
for /f usebackq^ tokens^=2^ delims^=^" %%A in ("input.txt") do echo "%%A"
)
If you want to make sure that the closing quote is present, then you can add FINDSTR to your DO clause. FINDSTR expects quotes to be escaped as \"
.
@echo off
>"output.txt" (
for /f usebackq^ tokens^=2^ delims^=^" %%A in ('findstr \".*\" "input.txt"') do echo "%%A"
)
The above solutions only write the first quoted string from any line. Additional quoted strings are ignored.
But I usually use my JREPL.BAT regular expression text utility to manipulate text. It is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.
Assuming your PATH includes a folder that contains JREPL.BAT, then all you need is the following from the command line:
jrepl "\q.*?\q" $0 /x /jmatch /f input.txt /o output.txt
Since JREPL is a batch script, you need to use CALL JREPL if you use the command within another batch script.
Note that the above JREPL solution writes out every quoted string on a separate line, even if there are two quoted strings within the same source line. If you only want the first quoted string from any line, then the solution becomes
jrepl "(\q.*?\q).*" $1 /x /jmatch /f input.txt /o output.txt