Page 1 of 1

Read and change last symbols in text file

Posted: 10 Feb 2017 10:00
by slavger
Hi guys, I'm trying to change some characters in txt file, but It's too difficult for my mind :(

I have a text file with some data and I need to replace last hex numbers in each string - result should be = 0x5b - 0x02 (for first string respectively)

Code: Select all

10.36.215.1  01 01 02 e0 36 76 d6 9f 5b
10.36.215.2  01 01 02 e0 36 76 d6 a2 4d
10.36.215.3  01 01 02 e0 36 76 d6 a5 99
10.36.215.4  01 01 02 e0 36 76 d6 a6 13
10.36.215.5  01 01 02 e0 36 76 d6 9c cd
10.36.215.6  01 01 02 e0 36 76 d6 a5 ff
10.36.215.7  01 01 02 e0 36 76 d6 a4 7f
10.36.215.8  01 01 02 e0 36 76 d6 a2 17
10.36.215.9  01 01 02 e0 36 76 d6 a3 7f
10.36.215.10  01 01 02 e0 36 76 d6 a2 61
10.36.215.11  01 01 02 e0 36 76 d6 a2 83
10.36.215.12  01 01 02 e0 36 76 d6 a4 a3
10.36.215.13  01 01 02 e0 36 76 d6 a1 e7
10.36.215.14  01 01 02 e0 36 76 d6 a3 fd



so I try to do like this

Code: Select all

FOR /F "tokens=1,5-10" %%a IN (tmp1.txt) do (
 @echo  %%a wrong WWN: 1000%%b%%c%%d%%e%%f%%g
 @echo  %WRITE2SW1%>>runme.txt
 set hex=0x%%g
 set /a varh=hex - 0x2
 call :dec2hex %varh% results

 @echo  %%a correct WWN: 1000%%b%%c%%d%%e%%f%results%
)

but it doesn't work - set hex=0x%%g, where %%g last nubers in string. :?
Pls help me to fix my mistake.

Thank you.

Re: Read and change last symbols in text file

Posted: 10 Feb 2017 10:10
by Squashman
You are inside a code block so environmental variables need to be referenced with delayed expansion.

Re: Read and change last symbols in text file

Posted: 11 Feb 2017 08:57
by slavger
Squashman wrote:You are inside a code block so environmental variables need to be referenced with delayed expansion.

Thanks, a lot. Can you share a simple explanation, of course if you have some free times? :oops:
Anyway thanks!

Re: Read and change last symbols in text file

Posted: 11 Feb 2017 11:02
by aGerman
Variables in a command line or in a block of command lines enclosed into parentheses (such as the body of your FOR loop) will be expanded to their values only once. That is, before the command line or block is executed. In order to bypass this early expansion you have to enable the delayed variable expansion in a sub environment using

Code: Select all

setlocal EnableDelayedExpansion

Surrounding percent signs of variables have to be replaced with surrounding exclamation marks.

not working:

Code: Select all

@echo off

set "x=12345"
echo before: %x%

for /l %%i in (1 1 3) do (
  set "x=%%i"
  echo %x%
)

echo after: %x%

pause


working:

Code: Select all

@echo off

setlocal EnableDelayedExpansion
set "x=12345"
echo before: %x%

for /l %%i in (1 1 3) do (
  set "x=%%i"
  echo !x!
)

echo after: %x%

pause


Steffen

Re: Read and change last symbols in text file

Posted: 11 Feb 2017 11:21
by Aacini
I like this example:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "var=Old"
set "var=New" & echo Normal: "%var%"  Delayed: "!var!"


rem To exchange the values of two variables:
set "var1=%var2%" & set "var2=%var1%"

Antonio

Re: Read and change last symbols in text file

Posted: 13 Feb 2017 03:06
by slavger
Thanks guys. :!:

It works, but I find another issue - can't call a function from FOR loop.

Code: Select all

@echo off
setlocal enabledelayedexpansion

set a=5B
set /a xx=0x%a% -0x2
call :dec2hex %xx% result
echo DEC result %xx%
echo HEX result %result%

for /l %%i in ( 1 1 3) do (
set "var1=0x5%%i"
set /a var2=var1 - 0x02
echo result in DEC: !var2!
call :dec2hex %var2% results
echo result in HEX: !results!
)


in results I can't make DEC 2 HEX conversion.

Code: Select all

DEC result 89 
HEX result 59
result in DEC: 79
result in HEX:
result in DEC: 80
result in HEX:
result in DEC: 81
result in HEX:


I'm sorry guys, maybe I need some help againe.
Thanks.

Re: Read and change last symbols in text file

Posted: 13 Feb 2017 07:40
by Squashman
slavger wrote:Thanks guys. :!:

It works, but I find another issue - can't call a function from FOR loop.

Code: Select all

@echo off
setlocal enabledelayedexpansion

set a=5B
set /a xx=0x%a% -0x2
call :dec2hex %xx% result
echo DEC result %xx%
echo HEX result %result%

for /l %%i in ( 1 1 3) do (
set "var1=0x5%%i"
set /a var2=var1 - 0x02
echo result in DEC: !var2!
call :dec2hex %var2% results
echo result in HEX: !results!
)


in results I can't make DEC 2 HEX conversion.

Code: Select all

DEC result 89 
HEX result 59
result in DEC: 79
result in HEX:
result in DEC: 80
result in HEX:
result in DEC: 81
result in HEX:


I'm sorry guys, maybe I need some help againe.
Thanks.

So you still do not understand delayed expansion inside a code block which is what we tried to explain to you already.

Re: Read and change last symbols in text file

Posted: 14 Feb 2017 03:48
by slavger
Squashman wrote:So you still do not understand delayed expansion inside a code block which is what we tried to explain to you already.


Already finished my script. :roll:

Thank you for your patience.