Reputation: 13
This is my example input txt file content:
2 12 1 0 0 1
3 13 1 1 0 0
4 14 1 0 1 0
9 19 0 1 0 0
And I want to get this content:
1`{SEQ=2,ACK=12,CTL=(SYN,on)} ++
1`{SEQ=3,ACK=13,CTL=(FIN,on)} ++
1`{SEQ=4,ACK=14,CTL=(RST,on)} ++
1`{SEQ=9,ACK=19,CTL=(FIN,off)}
So I used batch script:
@echo off
for /f "tokens=1-6 delims= " %%a in (file.txt) do (
IF %%c==1 IF %%d==0 IF %%e==0 IF %%f==0 ECHO 1^`^{SEQ=%%a,ACK=%%b,CTL=^(ACK,on^)^} ^+^+>> SEG1to2.txt
IF %%c==1 IF %%d==1 IF %%e==0 IF %%f==0 ECHO 1^`^{SEQ=%%a,ACK=%%b,CTL=^(FIN,on^)^} ^+^+>> SEG1to2.txt
IF %%c==1 IF %%d==0 IF %%e==1 IF %%f==0 ECHO 1^`^{SEQ=%%a,ACK=%%b,CTL=^(RST,on^)^} ^+^+>> SEG1to2.txt
IF %%c==1 IF %%d==0 IF %%e==0 IF %%f==1 ECHO 1^`^{SEQ=%%a,ACK=%%b,CTL=^(SYN,on^)^} ^+^+>> SEG1to2.txt
IF %%c==0 IF %%d==1 IF %%e==0 IF %%f==0 ECHO 1^`^{SEQ=%%a,ACK=%%b,CTL=^(FIN,off^)^} ^+^+>> SEG1to2.txt
IF %%c==0 IF %%d==0 IF %%e==1 IF %%f==0 ECHO 1^`^{SEQ=%%a,ACK=%%b,CTL=^(RST,off^)^} ^+^+>> SEG1to2.txt
IF %%c==0 IF %%d==0 IF %%e==0 IF %%f==1 ECHO 1^`^{SEQ=%%a,ACK=%%b,CTL=^(SYN,off^)^} ^+^+>> SEG1to2.txt
)
But of course I get this output:
1`{SEQ=2,ACK=12,CTL=(SYN,on)} ++
1`{SEQ=3,ACK=13,CTL=(FIN,on)} ++
1`{SEQ=4,ACK=14,CTL=(RST,on)} ++
1`{SEQ=9,ACK=19,CTL=(FIN,off)} ++
So I need delete last two "++". How can this be done? I don´t know which option in batch file will be used, and how many times.
Any help would be appreciated. Thanks!
Upvotes: 1
Views: 489
Reputation: 130929
Instead of testing each permutation with IF statements, you can define variables with the permutation built into the name, and the desired output in the value. Then you can simply use delayed expansion to print out the correct value.
@echo off
setlocal enableDelayedExpansion
set "map1001=CTL=(SYN,on)} ++"
set "map1100=CTL=(FIN,on)} ++"
set "map1010=CTL=(RST,on)} ++"
set "map0001=CTL=(SYN,off)}"
set "map0100=CTL=(FIN,off)}"
set "map0010=CTL=(RST,off)}"
for /f "tokens=1-6 delims= " %%A in (test.txt) do (
if defined map%%C%%D%%E%%F echo 1`{SEQ=%%A,ACK=%%B,!map%%C%%D%%E%%F!
)
If the ++
is to appear on all lines except for the last line, then a little more logic is needed.
@echo off
setlocal enableDelayedExpansion
set "map1001=CTL=(SYN,on)}"
set "map1100=CTL=(FIN,on)}"
set "map1010=CTL=(RST,on)}"
set "map0001=CTL=(SYN,off)}"
set "map0100=CTL=(FIN,off)}"
set "map0010=CTL=(RST,off)}"
set "ln="
for /f "tokens=1-6 delims= " %%A in (test.txt) do (
if defined map%%C%%D%%E%%F (
if defined ln echo !ln! ++
set "ln=1`{SEQ=%%A,ACK=%%B,!map%%C%%D%%E%%F!"
)
)
if defined ln echo !ln!
Upvotes: 3