how to set the next characters on a line as a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

how to set the next characters on a line as a variable

#1 Post by Rileyh » 30 Oct 2011 05:19

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to set the next characters on a line as a variable

#2 Post by dbenham » 30 Oct 2011 12:55

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
Last edited by dbenham on 30 Oct 2011 21:18, edited 1 time in total.

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: how to set the next characters on a line as a variable

#3 Post by Rileyh » 30 Oct 2011 20:32

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to set the next characters on a line as a variable

#4 Post by dbenham » 30 Oct 2011 21:18

Oops - I forgot the very important "DELIMS=" option in the FOR loop. I fixed the code above.

Dave Benham

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: how to set the next characters on a line as a variable

#5 Post by Rileyh » 30 Oct 2011 21:55

Thanks for the help.
Now, i noticed your strange syntax in regards to the "echo" section:

Code: Select all

echo(!ln:*%search%=!


Could you explain that to me?

Thanks again,
Rileyh

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to set the next characters on a line as a variable

#6 Post by dbenham » 30 Oct 2011 22:14

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

Post Reply