Reading a specific character in a line and comparing it.
Moderator: DosItHelp
-
- Posts: 22
- Joined: 26 May 2011 07:56
Reading a specific character in a line and comparing it.
If I leave the fourth line out of this batch file, every line gets copied to the output file, as they should. I'm trying to read the first character of the line and, only if it is equal to the letter A, copy the line to the output file.
There is obviously more ultimately, I am just going slow because I want to really understand how this all works. If I put in the line, the whole thing blows up. What am I doing wrong?
for /f "tokens=*" %%a in (infile.txt) do (
setlocal enableDelayedExpansion
set z=%%a
if !z:~0,1!==A do (
echo !z!>>outfile.txt
)
)
There is obviously more ultimately, I am just going slow because I want to really understand how this all works. If I put in the line, the whole thing blows up. What am I doing wrong?
for /f "tokens=*" %%a in (infile.txt) do (
setlocal enableDelayedExpansion
set z=%%a
if !z:~0,1!==A do (
echo !z!>>outfile.txt
)
)
Re: Reading a specific character in a line and comparing it.
DO is the wrong syntax for an IF statement and you have to enclose the left and the right string in quotes because of a wrong interpretation of the tokenizer.
If I had to write such a loop I would do it like that:
But actually I would use a FINDSTR one-liner.
Regards
aGerman
If I had to write such a loop I would do it like that:
Code: Select all
>outfile.txt type nul
for /f "usebackq tokens=* delims=" %%a in ("infile.txt") do (
setlocal enableDelayedExpansion
set "z=%%a"
if "!z:~0,1!"=="A" (
>>"outfile.txt" echo(!z!
)
endlocal
)
But actually I would use a FINDSTR one-liner.
Code: Select all
>"outfile.txt" findstr /b "A" "infile.txt"
Regards
aGerman
-
- Posts: 22
- Joined: 26 May 2011 07:56
Re: Reading a specific character in a line and comparing it.
aGerman wrote:DO is the wrong syntax for an IF statement and you have to enclose the left and the right string in quotes because of a wrong interpretation of the tokenizer.
If I had to write such a loop I would do it like that:Code: Select all
>outfile.txt type nul
for /f "usebackq tokens=* delims=" %%a in ("infile.txt") do (
setlocal enableDelayedExpansion
set "z=%%a"
if "!z:~0,1!"=="A" (
>>"outfile.txt" echo(!z!
)
endlocal
)
But actually I would use a FINDSTR one-liner.Code: Select all
>"outfile.txt" findstr /b "A" "infile.txt"
Regards
aGerman
Findstr is awesome. Thank you!
Okay, so if I want to change one character in the line before I write it to the output file, what is the proper syntax?
Re: Reading a specific character in a line and comparing it.
Don't get it. Could you explain that with an example line?
Regards
aGerman
Regards
aGerman
Re: Reading a specific character in a line and comparing it.
Hi mid_life_crisis,
I suppose you want to do something like this...
Print only all lines with "A" as first character, but add a "#" to the line.
It's useful to set line=%%a before the DelayedExpansion is switched on, else you could lose the "!".
jeb
I suppose you want to do something like this...
Print only all lines with "A" as first character, but add a "#" to the line.
Code: Select all
@echo off
setlocal DisableDelayedExpansion
(
for /f "usebackq tokens=* delims=" %%a in ("infile.txt") do (
set "line=%%a"
setlocal enableDelayedExpansion
if "!line:~0,1!"=="A" (
set "line=!line!#"
echo(!line!
)
endlocal
)
) > outfile.txt
It's useful to set line=%%a before the DelayedExpansion is switched on, else you could lose the "!".
jeb
-
- Posts: 22
- Joined: 26 May 2011 07:56
Re: Reading a specific character in a line and comparing it.
jeb wrote:Hi mid_life_crisis,
I suppose you want to do something like this...
Print only all lines with "A" as first character, but add a "#" to the line.Code: Select all
@echo off
setlocal DisableDelayedExpansion
(
for /f "usebackq tokens=* delims=" %%a in ("infile.txt") do (
set "line=%%a"
setlocal enableDelayedExpansion
if "!line:~0,1!"=="A" (
set "line=!line!#"
echo(!line!
)
endlocal
)
) > outfile.txt
It's useful to set line=%%a before the DelayedExpansion is switched on, else you could lose the "!".
jeb
I am curious, why does the SET line not need the "!" ?
This is close, but rather than add a character to the end of the line, I need to change a specific character in the line.
So maybe something like this?
Code: Select all
@echo off
setlocal DisableDelayedExpansion
(
for /f "usebackq tokens=* delims=" %%a in ("infile.txt") do (
set "line=%%a"
setlocal enableDelayedExpansion
if "!line:~0,1!"=="A" (
set "line:~79,1=A"
echo !line!>>outfile.txt
)
endlocal
)
)
Re: Reading a specific character in a line and comparing it.
mid_life_crisis wrote:This is close, but rather than add a character to the end of the line, I need to change a specific character in the line.
So maybe something like this?
Then you need to split the line into two parts (pre and post)
Code: Select all
set "line=!line:~0,15!*!line:~16!"
Changes only the 15th character to a "*"
jeb
-
- Posts: 22
- Joined: 26 May 2011 07:56
Re: Reading a specific character in a line and comparing it.
I got it. I think I even understand most of it, thanks to having a functional example and the wonders of Google to look up what you were doing.
I thank you both for the help.
I have another question in the same project but it is different enough from this that it should be its own topic, so I'll open a new thread for it.
I thank you both for the help.
I have another question in the same project but it is different enough from this that it should be its own topic, so I'll open a new thread for it.