Hello all i have an issue. i have an automatic txt file generated everyday from external company. and i have a bug when i want to load it into my system, so end users need to rewrite the file manually.
So i need to create a script with the following goal.
for each line which start by :86:/TYPE/0040/
i need to replace /TYPE/0040
by the value of the next line starting by /OCMT
example
86:/TYPE/0040/VOR ETRANGE / ==> 86:/OCMT/EUR2405,/VOR ETRANGE /
thanks a lot for any support script must run on Windows os.
this is an example of the file, where I have more than 2000 lines
##################
:86:/TYPE/0040/VOR ETRANGE /
/ORDP/GALAK CHEZ MAMANLDA
/ORDP/PT
/REMI/TOTOT TATA
/OCMT/EUR2405,/
/EFEE/EUR0,/
:86:/TYPE/0568/VOR/
/REMI//ADV/260050000002 15.1.2015
/ETEI/7026150001
/ORDP/SOC DES POUBELLES
:61:1000000005C20546,28NTRFNONREF//15014215
:86:/TYPE/0568/VOR /
/REMI/00000 0000000 0000000
/ETEI/VIREMENT OREXAD
/ORDP/ORETOPEZZ
:86:/TYPE/0040/VOR toto/
/ORDP/ADVANCED +
/ORDP/US
/REMI/200000000559ISCW APDORTSB
/OCMT/USD37500,/
/EFEE/EUR0,/
##################
I think I could make it! It is pure windows cmd version (No powershell involved). Here is the not trivial solution (I assume that the input file is called conv.txt
):
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
REM Read all lines and store OCMT values in environment variables
set REF=
REM set line=0
FOR /F "tokens=1,2,3 delims=/" %%i IN (conv.txt); DO (
SET /A line += 1
REM echo !line!: %%i, %%j, %%k
set type=%%i
if "!type:~0,1!" == ":" set REF=
IF "%REF%" == "" (
IF "%%i" == ":86:" IF "%%j" == "TYPE" IF "%%k" == "0040" set REF=!line!
)
if "%%i" == "OCMT" set OCMT_!!REF!!=%%j
)
REM Read all lines again and replace
set line=0
FOR /F "tokens=* delims=" %%i IN (conv.txt); DO (
SET /A line += 1
set LL=%%i
IF "!LL:~0,15!" == ":86:/TYPE/0040/" (
FOR /F "tokens=1* delims==" %%a IN ('set OCMT_!line!') DO (
set LL=:86:/OCMT/%%b/!LL:~15!
)
)
echo !LL!
)
Output:
##################
:86:/OCMT/EUR2405,/VOR ETRANGE /
/ORDP/GALAK CHEZ MAMANLDA
/ORDP/PT
/REMI/TOTOT TATA
/OCMT/EUR2405,/
/EFEE/EUR0,/
:86:/TYPE/0568/VOR/
/REMI//ADV/260050000002 15.1.2015
/ETEI/7026150001
/ORDP/SOC DES POUBELLES
:61:1000000005C20546,28NTRFNONREF//15014215
:86:/TYPE/0568/VOR /
/REMI/00000 0000000 0000000
/ETEI/VIREMENT OREXAD
/ORDP/ORETOPEZZ
:86:/OCMT/USD37500,/VOR toto/
/ORDP/ADVANCED +
/ORDP/US
/REMI/200000000559ISCW APDORTSB
/OCMT/USD37500,/
/EFEE/EUR0,/
##################
It parses the input file twice. First block reads the file and sets an environment variable OCMT_<TYPE_LINE>=<OCMT LINE AMOUNT>
(eg. OCMT_2=EUR2405,
). In the second block it finds again the lines starting with :86:/TYPE/0040/
. Then it replaces with :86:/OCMT/<OCMT LINE AMOUNT>/<rest of the line>
.
The main trick was how to get back the environment variables. It was solved be the internal FOR
loop calling set
in cmd .
This is not a complete solution! Not all possible error is checked and handled (eg only :86:/TYPE/0040/...
lines may contain OCMT
sublines? Could it have empty lines?), but it fits to the brief specification...
I hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.