Replace a matching string in each line with another string - batch-file

I am trying to replace the number at the end of every line of a given file with an incremented number(+1).
My Input file looks like
C:\documents\a.txt,1988,15.01.00.0059
C:\documents\we.txt,1988,15.01.00.0059
C:\documents\gh.txt,1987,1988,15.01.00.0059
my out put file should be
C:\documents\a.txt,1988,15.01.00.0060
C:\documents\we.txt,1988,15.01.00.0060
C:\documents\gh.txt,1988,15.01.00.0060
My attempt is like
#echo off
setlocal enableextensions disabledelayedexpansion
set "search="
for /F "tokens=1-3 delims=," %%i in (%1) do (
#echo %%i %%j %%k
set "search=%%k"
)
set "replace="
REM #echo %replace%
set "textFile=%1"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%textFile%" echo(!line:%search%=%replace%!
endlocal
)
i could able to get 15.01.00.0059 from the input file but dont know how to change to 15.01.00.0060 .
replacing to the same input file is also working . I just need to know how to increment the value and assign to "replace".
Thank you
Bhargav.k

This should be flexible enough:
#echo off
setlocal EnableDelayedExpansion EnableExtensions
1>out.txt (
FOR /F tokens^=*^ delims^=.^ eol^= %%L in (input.txt) do (
set "line=%%L"
set "x=!line:.= !"
FOR %%T in (!x!) do set "lastToken=%%T"
cmd /c exit /b !lastToken!
set/a"lstrip=!ERRORLEVEL!+1"
>getlen.tmp echo(!lasttoken!
FOR %%L in (getlen.tmp) do set/a"len=%%~zL-2" %= get length of last token, minus CRLF =%
FOR %%N in (1 1 !len!) do set "lstrip=0!lstrip!" PREFIX lstrip with zeroes
FOR %%N in (!len!) do (
set "lstrip=!lstrip:~-%%N!"
echo !line:~0,-%%N!!lstrip!
)
)
)
del getlen.tmp
ENDLOCAL
This is not a straightforward approach.
The script counts the # of zeroes prefixed before the last token by the ERRORLEVEL returned by EXIT /B, compared with the original token.

Related

Batch remove part of string after “-1” is found or any other number “-[0-9]”

I got a file containing a string on each line like this:
fruit-apple-1.5.6
vegtable-sla-mc5-6.5-16515
extra-huh-9.5-511-515
extra-3.2
I am iterating over it and want it to remove the part of the string on the right after in find "-" + any number "-0","-1","-2","-9",...
so output should be
fruit-apple
vegtable-sla-mc5
extra-huh
extra
this is code i have but it only works with a "-" i cant combine it so it takes "-" + any number like "-1","-5","-2",...
for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
echo stripped string = %%A
)
complete code not necessary i think but in case u need it below
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set "RawPath=%~dp0"
FOR /F "USEBACKQ TOKENS=*" %%M IN ("%RawPath%/mods") DO (
REM for %%f in (*.jar) do (
Set "fileNameCheck=%%M"
for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
Echo [46m%%A[0m
if exist "%~dp0%%A*.jar" (
REM echo [32mFound %%A "%~dp0%%A*.jar"[0m
if exist "%~dp0%%M" (
REM echo [42mUp to Date[0m [32m%%A "%~dp0%%M"[0m
) else (
for %%j in (*.jar) do (
echo %%j |find "%%A" >nul
if not errorlevel 1 (
echo [41mDifferent Version[0m [31m%%j [0m[90mNewer version[0m [32m%%M[0m
)
)
)
) else (
REM echo [31mMissing %%A[0m
)
)
)
pause
Given that the strings do not contain any of the caracters ?, *, < and >, the following script should do the trick:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~dp0."
set "_FILE=mods.txt"
rem // Read file line by line:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
rem // Store current line, initialise variables:
set "LINE=%%L" & set "PREV=-" & set "COLL=-"
setlocal EnableDelayedExpansion
rem /* Split line string at every `-` and iterate through items;
rem (`?`, `*`, `<`, `>` must not occur in the string!): */
for %%I in ("!LINE:-=" "!") do (
endlocal & set "ITEM=%%~I"
rem // Check whether item only consists of numerals and `.`:
(
rem // This loop executes when there are other characters:
for /F "delims=0123456789. eol=." %%J in ("%%~I") do (
setlocal EnableDelayedExpansion
rem /* Rebuilt string with currently found items; if there
rem are other items following numeric ones, keep them: */
for /F "delims=" %%K in ("!COLL!!PREV:~1!-!ITEM!") do (
endlocal & set "COLL=%%K" & set "PREV=-"
)
)
) || (
rem // This section runs when there are numerals and `.`:
setlocal EnableDelayedExpansion
for /F "delims=" %%K in ("!PREV!-!ITEM!") do (
rem /* Store numeric items in a separate buffer; if there
rem are no other items following, this is dismissed: */
endlocal & set "PREV=%%K"
)
)
setlocal EnableDelayedExpansion
)
rem // Return rebuilt line string:
echo(!COLL:~2!
endlocal
)
endlocal
exit /B
The example input file mods.txt:
fruit-apple-1.5.6
vegtable-sla-mc5-6.5
extra-huh-9.5
extra-3.2
test-9.15.5
keep-forge
name-subname-10.5-55.5
globalxp-1.16.5
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more-3.4
mix-1.2-string-10.5-55.5-more-3.4-5.6-7
8.9
1.2.3-lead
1.2.3-4.5.6-7.8.9-lead-mult
leads to the following output:
fruit-apple
vegtable-sla-mc5
extra-huh
extra
test
keep-forge
name-subname
globalxp
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more
1.2.3-lead
1.2.3-4.5.6-7.8.9-lead-mult
One way is to replace the - with \ and then use variable substitution to strip the %%~ni part from the string. We just need to remove the first and last occurrence then from the string. findstr will determine that the end of string needs to be -num.*
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b /s *.jar ^| findstr /RC:"-[0-9].*$"') do (
set "line=%%~nxA"
for %%i in ("!line:-=\!") do set "final=%%~pi"
set "final=!final:~0,-1!-"
echo !final! | findstr /RC:"\\[0-9].*$" >nul 2>&1
if !errorlevel! equ 0 (
for %%s in ("!final!") do set "final=%%~ps"
set final=!final!
)
set "final=!final:~0,-1!"
set "final=!final:~1!"
echo !final:\=-!
)
the edited code should take care of the additional - as per your comment example name-subname-10.5-55.5
Here's an untested idea, which may not be particularly quick, especially if you have a large number of strings in your mods file:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F %%G In ('Echo Prompt $E ^| %SystemRoot%\System32\cmd.exe /D'
) Do Set "$E=%%G"
For /F Delims^=^ EOL^=^ UseBackQ %%G In ("%~dp0mods") Do (Set "Line=%%G"
SetLocal EnableDelayedExpansion
For /L %%H In (0 1 9) Do For /F Delims^=^ EOL^= %%I In (
'(Echo(^) ^| Set /P "=!Line:-%%H="^&:"!" 0^<NUL') Do Set "Line=%%I"
Echo(%$E%[46m!Line!%$E%[0m
EndLocal
)
Pause

Removing carriage returns from a text file using a batch file based on a value

I have a text file that I would like to edit and therefore would like to remove the last line. I have the following code for this:
for /f "delims=" %%a in (input.txt) do (
echo/|set /p ="%%a%"
)>>output.txt
input:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
output:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Now I would like to edit the data in groups for example by the first value, so that I have the following output:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
If I replace the FOR /F "delims=" %%a in (input.txt) do … loop with an equivalent FOR %%a in … loop:
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "_gruppeName="
(
for %%a in (
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
) do (
for /f "tokens=1 delims=;" %%A in ("%%~a") do (
if /I NOT "%%~A"=="!_gruppeName!" (
if defined _gruppeName echo(
set "_gruppeName=%%~A"
)
)
echo/|set /p ="%%~a"
)
echo(
)>>output.txt
REM debugging output follows
type output.txt
Output:
1st run: 2>NUL del output.txt & D:\bat\CR\61816520.bat
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Next run: D:\bat\CR\61816520.bat
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Your question is not clear
based on ... the first value (GRUPPEA)
is it SORTed? or just write duplicates on the same line?
#echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
::VARIABLES
set "in=input.txt" IN file
set "out=output.txt" OUT file
set/a"#lines=0"
set "gruppe="
set "prevContent="
::Count lines
FOR /F %%L in ('
"findstr /N "^^" "%in%" %= do not skip empty lines =%"
') do set/a"#lines+=1" %= Get the # of lines =%
::Read IN via SET /P #LINES number of times
::Bangs (!) will be lost
<"%in%" >"%out%" (FOR /L %%# in (1 1 %#lines%) do (
set "data=" ::clear DATA
set/p"data=" ::read from IN
FOR /F tokens^=1^ delims^=^;^ eol^= %%T in ("!data!") do set "gruppe=%%T"
if NOT "!prevContent!" == "!gruppe!" (
set "prevContent=!gruppe!"
echo(
)
<nul set/p"=!data!" ::does not work with leading space, tabs, or equal signs
)) %= read file by lines via SET /P =%
exit /b
The script counts the number of lines using FINDSTR /N and a FOR /F loop to count the # of lines, which is required to execute SET /P that many times.
Tips:
Use ECHO( instead of the problematic ECHO.
Using a pipe | is very slow, as described by #jeb here. Use <nul instead

Bat file to remove data from text file

I have bat file which split my data from one row into multiple rows as expected but it is taking unnecessary data as well.
Data in txt file-
Data will be in same format as below
85:85123, ,Smith,181215:image.tif,image2.tif,image3.tif
85:85456, ,Richard,184985:image4.tif,image5.tif
85:85789, ,Aly,589456:image.tif
Output I am getting is:
123,
123,smith
123,image.tif
123,image2.tif
123,image3.tif
456,
456,Richard
456,image4.tif
456,image5.tif
789,
789,Aly
789,image.tif
Output which i want:
123,image.tif
123,image2.tif
123,image3.tif
456,image4.tif
456,image5.tif
789,image.tif
I only want unique number with image name on every row but I am getting unique number with one blank value and first name as well.
#echo off
> "Output.csv" (
for /F "usebackq tokens=1-2* delims=: " %%A in ("transact.txt") do (
set "NUM=%%B" & set "LIST=%%C"
setlocal EnableDelayedExpansion
for %%D in ("!NUM:*%%A=!") do (
for %%E in ("!LIST:,=" "!") do (
endlocal
echo(%%~D %%~E
setlocal EnableDelayedExpansion
)
)
endlocal
)
)
Well, you seem to want to filter out all files whose names are not image followed by an optional number and followed by .tif, which could be done by inserting a bit of code (see rem remarks):
#echo off
> "Output.csv" (
for /F "usebackq tokens=1-2* delims=:, " %%A in ("transact.txt") do (
set "NUM=%%B" & set "LIST=%%C"
setlocal EnableDelayedExpansion
for %%D in ("!NUM:*%%A=!") do (
for %%E in ("!LIST:,=" "!") do (
endlocal
rem // Store several interim values into variables:
set "ITEM=%%~nE" & set "EXT=%%~xE" & set "NUM=%%~D"
setlocal EnableDelayedExpansion
rem /* Split file name at first numeric part; the preceding `_` just
rem ensures that the part before the first number is not empty,
rem because then files like `012image3.tif` became handled wrongly;
rem everything up to the first colon `:` becomes removed: */
for /F "tokens=1* delims=0123456789" %%I in ("_!ITEM:*:=!") do (
endlocal
rem /* Ensure that the part behind the first numeric part (`%%J`) is
rem empty, that the name part in front of the number is `image`,
rem and that the file extension is `.tif`: */
if "%%J" == "" if /I "%%I" == "_image" if /I "%%~xE" == ".tif" (
setlocal EnableDelayedExpansion
echo(!NUM!,!ITEM:*:=!!EXT!
endlocal
)
setlocal EnableDelayedExpansion
)
)
)
endlocal
)
)
Just 3 minimal changes are necessary, to get your desired output.
:: Q:\Test\2019\09\09\SO_57856028.cmd
#echo off
> "Output.csv" (
for /F "usebackq tokens=1-4* delims=:, " %%A in ("transact.txt") do (
set "NUM=%%B" & set "LIST=%%E"
setlocal EnableDelayedExpansion
for %%D in ("!NUM:*%%A=!") do (
for %%E in ("!LIST:,=" "!") do (
endlocal
echo(%%~D,%%~E
setlocal EnableDelayedExpansion
)
)
endlocal
)
)

How can I extract part of a string based on a pattern using Windows CMD?

I have an input let's say:
DummyString-v1.0.0
and I would like to extract everything after v to get: 1.0.0
How can I achieve this in CMD?
#ECHO OFF
SETLOCAL
SET "string=DummyString-v1.0.0"
SET "string=%string:*v=%"
ECHO %string%
GOTO :EOF
where the :*substring=replacementstring syntax is applied meaning "every character form the start of the variable (string) up to the substring (v) is replaced by the replacement string (nothing).
#echo off
call :split "DummyString-v1.0.0" "v"
echo %last_part%
exit /b 0
:split
set "string=%~1"
set "splitter=%~2"
setlocal EnableDelayedExpansion
set LF=^
rem ** Two empty lines are required
echo off
for %%L in ("!LF!") DO (
for /f "delims=" %%R in ("!splitter!") do (
set "var=!string:%%R=%%L!"
)
)
for /f "delims=" %%P in (""!var!"") DO set "last_part=%%~P"
endlocal &(
set last_part=%last_part%
exit /b 0
)

Splitting txt using batch containing "=" on each line

I'm splitting the file every 6000 lines with the script below but I'm having a problem with it....
Every line of my txt file has the character '=' so when the batch is treating the file, where I have the '=' the line stops and goes to the next one (I don't have the end of each line, anything that follows the = is deleted).
FOR %%X IN (*.TXT) do (
setlocal ENABLEDELAYEDEXPANSION
SET BFN=%%X
SET LPF=6000
SET SFN=%%X_6000_
)
REM ==============================
SET SFX=%BFN:~-3%
SET /A LineNum=0
SET /A FileNum=1
For /F "delims==" %%l in (%BFN%) Do (
SET /A LineNum+=1
echo %%l >> %SFN%!FileNum!.%SFX%
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
endlocal
Can Someone help me out?
Sample of file:
A88A0A1891BAA9=B088A00000003800001==00000038000
A88A0A1B13A0BB=B088A00000089000002==00000890000
A88A0A1A13B830=B088A000000B3800003==00000B38000
A88A0A00331831=B088A00000010A00004==0000010A000
A88A0A10B31B39=B088A00000090A00005==0000090A000
A88A0A19A3AA89=B088A00000089800006==00000898000
A88A0A19AA0318=B088A000000A0100007==00000A01000
A88A0A08911913=B088A00000008800008==00000088000
A88A0A1089A139=B088A00000098300009==00000983000
A88A0A1BB8BBA8=B088A000000AA100010==00000AA1000
A88A0A0A8B9199=B088A00000098100011==00000981000
A88A0A19AA8A9B=B088A00000088900012==00000889000
A88A0A0B380A13=B088A00000099A00013==0000099A000
A88A0A13899A18=B088A00000088A00014==0000088A000
A88A0A1A188910=B088A0000008A800015==000008A8000
A88A0A10930AA0=B088A0000009B900016==000009B9000
A88A0A09338A88=B088A000000A0A00017==00000A0A000
A88A0A11A98930=B088A000000AAB00018==00000AAB000
Setting the LPF (lines Per File) to 6, here is the result (first of 3 files):
A88A0A1891BAA9
A88A0A1B13A0BB
A88A0A1A13B830
A88A0A00331831
A88A0A10B31B39
A88A0A19A3AA89
Thanks!
Remove the second = in the delims :
For /F "delims=" %%l in (%BFN%) Do (
delims== means the command tries to split each line with =
What you need is
For /F "tokens=*" %%l in (%BFN%) Do
This means "give me the whole line without splitting it into tokens"
Also in your code above the nesting of the do ( ) blocks seems wrong.
It looks like the first for loop simply sets environment variables and does nothing else. Did you actually run this code?
It should be
#echo off
FOR %%X IN (*.TXT) do (
setlocal ENABLEDELAYEDEXPANSION
SET BFN=%%X
SET LPF=6000
SET SFN=%%X_6000_
REM ==============================
SET SFX=%BFN:~-3%
SET /A LineNum=0
SET /A FileNum=1
For /F "tokens=*" %%l in (%BFN%) Do
SET /A LineNum+=1
echo %%l >> %SFN%!FileNum!.%SFX%
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
endlocal
)

Resources