batch file - Removing everything after/before the first occurrence of a given string -
i working on small utility designed split file in 2 after first occurrence of given delimiter, single input file, 2 smaller output files.
for example, lets assume have input file contents:
alpha bravo charlie charlie echo
and delimiter using charlie
, how pair of output files like:
alpha bravo
and
<blank line> charlie echo
i working along lines of finding first occurrence of delimiter string, performing substring on original text twice, using delimiter string position before split text, gets saved output. however, while have been successful in finding out how substrings, have not had success finding out strpos
function.
so, can strpos result input block of text/file path, or there (better?) method of doing trying do?
n.b. unable use beyond windows batch script this, while php, or sed, or java, not option
here's solution. can configure output files in _outfile1
, _outfile2
variables, split token in _split
. input file input.txt
assumes content you've stated in question.
@echo off setlocal enabledelayedexpansion set _outfile1=output1.txt set _outfile2=output2.txt set _split=charlie set of=!_outfile1! /f "tokens=*" %%a in ( input.txt ) ( set test=%%a if "!of!"=="!_outfile1!" ( if "!test:%_split%=!"=="%%a" ( echo %%a >> !of! ) else ( echo !test:%_split%=! >> !of! set of=!_outfile2! echo. >> !of! ) ) else ( echo %%a >> !of! ) )
Comments
Post a Comment