Findstr, problem with $ anchor

  • Thread starter Thread starter Deasun
  • Start date Start date
D

Deasun

On an XP system, the $ anchor (match token at end of line) does not
seem to work.
Example;
@echo off
set target_string=abc
echo Verifying target_string: *%target_string%*
echo %target_string% | findstr /R "^abc$"
if %errorlevel% == 0 (
echo A hit, found a match
) else (
echo No match found
)
exit /b
 
On an XP system, the $ anchor (match token at end of line) does not
seem to work.
Example;
@echo off
        set target_string=abc
        echo Verifying target_string: *%target_string%*
        echo %target_string%  | findstr /R  "^abc$"
        if %errorlevel% ==  0 (
                echo A hit, found a match
        ) else (
                echo No match found
        )
        exit /b
This gives the result ;
C:\BAT>test_findstr
Verifying target_string: *abc*
No match found

C:\BAT>
The ^ anchor works. I have searched on-line and found no report of
anybody having a problem with the $ anchor. I suspect it might be due
to a end of line terminator (CR LF), but am not aware of any command
such as the Perl chomp to remove line feeds. Would appreciate if
somebody could run my code snippet or offer explanation
 
This gives the result ;
C:\BAT>test_findstr
Verifying target_string: *abc*
No match found

C:\BAT>
The ^ anchor works. I have searched on-line and found no report of
anybody having a problem with the $ anchor. I suspect it might be due
to a end of line terminator (CR LF), but am not aware of any command
such as the Perl chomp to remove line feeds. Would appreciate if
somebody could run my code snippet or offer explanation


Two issues: You are echoing "%target_string%  " which has two trailing spaces
before the pipe character.

"if %errorlevel% ==  0" should really be "if %errorlevel% EQU 0" or
"if not errorlevel 1" or "if %errorlevel%.==0." to avoid ambiguity or errors.
 
Two issues:  You are echoing "%target_string%  " which has two trailing spaces
before the pipe character.

"if %errorlevel% ==  0" should really be "if %errorlevel% EQU 0" or
"if not errorlevel 1" or "if %errorlevel%.==0." to avoid ambiguity or errors.- Hide quoted text -

- Show quoted text -

Thank you. You are quite right. It was the space before the pipe
character that was causing the problem. I had also tested by
directing the target string into a disk file i.e.
echo %target_string > input.txt
but in exactely the same manner I was inadvertently appending a SPACE
at the end of the line.

Its a relief to have a simple explanation to what was a perplexing
problem. I will be far more careful typing in batch code in the future
 
Two issues: You are echoing "%target_string% " which has two trailing
spaces
before the pipe character.

"if %errorlevel% == 0" should really be "if %errorlevel% EQU 0" or
"if not errorlevel 1" or "if %errorlevel%.==0." to avoid ambiguity or
errors.- Hide quoted text -

- Show quoted text -

Thank you. You are quite right. It was the space before the pipe
character that was causing the problem. I had also tested by
directing the target string into a disk file i.e.
echo %target_string > input.txt
but in exactely the same manner I was inadvertently appending a SPACE
at the end of the line.

Its a relief to have a simple explanation to what was a perplexing
problem. I will be far more careful typing in batch code in the future

===> glad you got that one sorted out. What I was going to suggest was that
the caret "^" has also a special meaning for cmd.exe: "literal next
character". Basically, it plus the following character are replaced with
just the following character, bypassing intermediate interpretation, i.e.:

for /f "delims=~" %%X in ('dir C:\ | find ":" ') do echo %%X

will generate a "| was unexpected at this time" error, whereas this:

for /f "delims=~" %%X in ('dir C:\ ^| find ":" ') do echo %%X

doesn't. I was concerned that perhaps cmd.exe was converting ["|abc$"] into
["abc$"] before passing the result to findstr.exe.

/Al
 
Back
Top