Page 1 of 1
how to set the next characters on a line as a variable
Posted: 30 Oct 2011 05:19
by Rileyh
Hi all,
I want to know how to get a batch file to echo the characters that occur after a point but on the same line. The batch file needs to find a string of text and then echo the characters that occur on the same line, but after it from left to right.
For example:
(IN A TEXT FILE)
Hello world
The batch file will need to find "Hello" and then it should echo "world".
Any help would be greatly appreciated,
Regards,
Rileyh
Re: how to set the next characters on a line as a variable
Posted: 30 Oct 2011 12:55
by dbenham
Your requirements don't match your example output - what about the space between "Hello" and "world"
For this example I'm going to assume the space should be appended to the search string, as in "Hello ".
Assume the file is named "file.txt". Here is a case insensitive solution.
Code: Select all
setlocal enableDelayedExpansion
set file="file.txt"
set "search=hello "
for /f %%A in ('findstr /i /c:"%search%" %file%') do (
set "ln=%%A"
echo(!ln:*%search%=!
)
The solution uses FINDSTR to quickly find lines with the string. The FOR /F captures the results. String substitution with the * prefix finds the string and replaces everything before the string and the string itself with nothing. Since string substitution is case insensitive, the FINDSTR uses the /I option to make sure it finds the same string as the string substitution.
The solution can be made pseudo case sensitive if the /I option is removed from the FINDSTR - but it has a risk. Assume you are looking for "Hello ", the FINDSTR will correctly ignore a line like "hello world". But a line like "hello Hello world" will incorrectly return "Hello world".
The above solution works well as long as the line containing the search string does not contain an exclamation point (!). If it does, then
SET "LN=%%A" will corrupt the results.
Here is a case insensitive solution that works if ! is a problem:
Code: Select all
setlocal disableDelayedExpansion
set file="file.txt"
set "search=hello "
for /f "delims=" %%A in ('findstr /i /c:"%search%" %file%') do (
set "ln=%%A"
setlocal enableDelayedExpansion
echo(!ln:*%search%=!
endlocal
)
One last limitation - both solutions will fail to return a value for any line beginning with a semicolon because of the implicit FOR /F "EOL=;" option. You can use
HOW TO: FOR /F Disabling EOF or using a quote as delim if this is a problem.
Dave Benham
Re: how to set the next characters on a line as a variable
Posted: 30 Oct 2011 20:32
by Rileyh
Thanks for the string, but it didn't work.
It echoed "hello" when the "search" variable" was set to "hello", instead of doing what it was supposed to do, which was echoing "world", since it occurred after "hello".
Could you fix it?
Regards,
Rileyh
Re: how to set the next characters on a line as a variable
Posted: 30 Oct 2011 21:18
by dbenham
Oops - I forgot the very important "DELIMS=" option in the FOR loop. I fixed the code above.
Dave Benham
Re: how to set the next characters on a line as a variable
Posted: 30 Oct 2011 21:55
by Rileyh
Thanks for the help.
Now, i noticed your strange syntax in regards to the "echo" section:
Could you explain that to me?
Thanks again,
Rileyh
Re: how to set the next characters on a line as a variable
Posted: 30 Oct 2011 22:14
by dbenham
The * replacement I explained in the original post. It is also described in HELP SET.
!ln:*%search%=! may result in an empty string, which would leave ECHO followed by nothing. This outputs ECHO is off.
In contrast, ECHO( will output a blank line
Dave Benham