Search code examples
batch-filebatch-processing

Batch replace data with variable at specific position in a txt


I have two goals. Firstly, I need to find some 16-character-string specifically positioned after the symbol @, which can be found somewhere inside my txt file, and save it to a variable called v1. Even thought there can be several "@", what is important is that I get the first "@" and this first "@" can be anywhere at random. Secondly, I need to replace this specific chunk of data found in v1 with another variable called v2. Both v1 and v2 are 16-character-long.

My file is at d:\msdos\testing.txt and it has around 3000 lines or even more (see example below). The user cannot have an easy access to its content. All the process of "deleting and replacing" should be done automatically. The other observation is that the characters around v2 (after replacement) have to be left untouched. By the way, the search character "@" is not on the first line. In fact it can me anywhere in the file.

adflkjsdf@12346dsf88dfsd1600vczxv0012554xzcxcz 54df 6d5s4f s5df4s fdsfsfsfsfsewfwefwfwfewfwƒÁ♣)é=E@tF¸ð♣·@t/ó=╣@uæQjèDó♣ÿÿYð·@t|,óÿÿYëû4^ü^ü╣Ë‹WüöÂuÊWüëøƒâðÓ×ú0è♫îôÿÿ_øCD.üû0.♣Úè♫õÿÿÆ·@ð]^Ã×ÐèÛøÿÿÀtÅÂðùèŽôÿÿð╣èGü╣ÿÿè]^ËGü¨íƒàð,9ê‡ß=E@t♫73¸♫ð·@t3ó=­ð·@tQRjèAòÿÿZYë¿»♣4^ü‹Gü¨t~ƒàð,9ê╣wt=0ôÿÿZYÈÁèÈ1ÿ)Ѓ×ÿ!Ó73▓Ãa▓ÃaÿÿÿƒÀ0U)Âw.÷ƒÅëT.üz|ú0ðèÿÿcd9f·@ð]_^ÃÆ·@d2cji▄♫♫f♣♫♫5b577i▄868dda11f81f72655a55ffa3fa7ce3a2c00cf♣w♣1T5d045opi4336578╣5d╣9d2▓╣╣▄f♣▄13120d694ss2014╣5ja♣♥1eFÃ♣╣9♪5♪eae╣♣5e♣eTab5b5▓777d2IUd04♪5♪8cd9d215d24♫b570♪22637d2ji▄7fd0458cd9fc702263a9fe6b7d73f1314♫bOIU¨&♫765d╣22637b5e6b7d73d♫80ji▄j513120da♣eTÃa§♥694♫O¶§♥I§♥♥q61g67d215▓♫b57022637♣d2♫15▓ab5d╣9d╣♣eTÃa♪5e6b7d73d2▄╣131cV6◙632s6I’’ƒùƒßÿÁèâÿƒÈ0ˆ’ƒùƒßÿÁèƒÈ0ˆG_Ã@B6♫23q♪2§♥1q♥♥5m5p2BÃ♫a♣1eFÃ♥f♣w♣1a1B5l5B5o987W♫LKJEee879d▓7d2╣▄╣╣╣1♥5▓24♫♥b57022637d╣9♣ji▄f♣b5d╣♣9d215▓ab5♫e6b7d7▓▓▓♫V▓▓♫3dji▄4♫b131022876♪86d215d21OIE98222╣tr2♪4♫b▓▓▓570226ƒÀÜ‹0‹ÃƒÀsçfpro79g235flks8971IU3120dÃ

The first part I was able to do it well, as is:

@echo off
SetLocal EnableDelayedExpansion
set v2=0000000022200000
set x=d:\msdos\testing.txt
for /f "delims=@ tokens=1*" %%A in (%x%) do set v0=%%B
set v1=%v0:~0,16%
echo %v1% & REM 12346dsf88dfsd16 

How can I accomplish the second goal?


Solution

  • Batch isn't the right tool for this. It can be done using REGEX, but batch has no implementation of this. Using dbenhams great jrepl.bat makes it easy though:

    @echo off
    SetLocal 
    set "v1=@[a-zA-Z0-9]{16}""  & REM this is a REGEX search pattern 
    set "v2=@0000000022200000"  & REM this is the replacement string
    call jrepl "%v1%" "%v2%" /F testing.txt /M /O -
    

    See jrepl.bat /? for usage info. In short:

    jrepl <searchstring> <replacementstring> <switches>
    

    where /F <inputfile> gives the file to change,
    /M keeps original line endings (might not be needed here, but to be sure...)
    /O - overwrites inputfile with the result. Instead of - you can also give a file name (/O testing.new)

    NOTE: This replaces each string in the file matching the search pattern.