I would like to use (gnuwin32) grep in a batch file
e.g.
set REGEX=^hello
echo util.exe /c grep.exe -e %REGEX% > myout.txt
util.exe /c | grep.exe -e %REGEX% > myout.txt
FOR /L %%i IN (0,1,100) DO (
util.exe /c | grep -e %REGEX% >> myout.txt
)
This should fetch all occurrences of lines which contains
an hello string at the _beginning_ of the line which comes
from the tool util.exe
Does anybody know how to use such a pattern
(especially with the "caret" ^) within gnuwin32 grep?
btw: How can I use the "|" and the ">" in an echo line?
Thomas
This seems to work here, in an echo statement anyway.
The problem is that ^ is an escape character - see how it is used in ^| in
the echo statement to make echo print a literal | to the screen.
@echo off
set "REGEX=^hello"
set "regex=%regex:^=^^^%"
echo util.exe ^| /c grep.exe -e %REGEX% ^>^> file.txt
pause
set regexp="^Hello"
echo 123 Hello | grep -e %regexp% 1>file.txt
echo Hello 123 | grep -e %regexp% 1>>file.txt
> This seems to work here, in an echo statement anyway.
> The problem is that ^ is an escape character - see how it is used in ^| in
> the echo statement to make echo print a literal | to the screen.
yes, that was the main problem. Thank you. It's working fine. But:
> set "regex=%regex:^=^^^%"
that line is a kind of magic to me.
1. Why do you write that in lowercase?
Because envvars are not case-sensitive?
Is that regex the _same_ var as REGEX? And could
I also write set "REGEX=%REGEX:^=^^^%"
2. What are doing the colon and the following
sequences of escape-chars and percent signs?
Can you tell mor about that construct?
Thomas
Looks good too to me. But actually I would
prefer the way posted by foxidrive bcause I
don't need "setlocal" and that would work
with earlier versions of cmd.exe/command.com
Would'nt it? Or/And ist that setlocal not important
to your echo constructs? I think that setlocal only
effects the validity of vars?
Can you explain the 1 (one) in that constructs and how
it's working?
In wich version of Windows was the "setlocal"
indtroduced?
Thomas
> 1. Why do you write that in lowercase?
> Because envvars are not case-sensitive?
> Is that regex the _same_ var as REGEX? And could
> I also write set "REGEX=%REGEX:^=^^^%"
they are not case sensitive. %regex% = %REGEX% = %ReGeX% = %rEgEx% =
%regeX%
> 2. What are doing the colon and the following
> sequences of escape-chars and percent signs?
> Can you tell mor about that construct?
String replacement. In string %regex%, replace every occurrence of ^
with ^^^
set string=%string:a=b%
replace all occurrences of 'a' in %string% with 'b'
>> [...]
>> Can you tell mor about that construct?
> String replacement. In string %regex%, replace every occurrence of ^
> with ^^^
> set string=%string:a=b%
> replace all occurrences of 'a' in %string% with 'b'
Thank you. Now that is clear.
Now I have an additional question
How can I pass a Regular Expression to a batch file as an
argument if that argument contains the escape character "^" ?
e.g.
rebatch.bat ^hell*ld
and inside the batch the RE "^hell*rld" should used with
grep and would fetch "hello world" or "hell is the world", etc.
Thomas
> How can I pass a Regular Expression to a batch file as an
> argument if that argument contains the escape character "^" ?
> rebatch.bat ^hell*ld
It is normal to employ experimentation to determine how many times a
character must be escaped to pass properly through your script. A
character might need to be escaped once to get to the script, a second
time to get to a subroutine, a third time foor that subroutine to call
something else; or perhps each step might require a double-escape.
Try:
rebatch.bat ^^hell*ld
rebatch.bat ^^^hell*ld
rebatch.bat ^^^^hell*ld
rebatch.bat "^hell*ld"
It the last case the quotes might need to be removed within the script
before the parameter is used:
Set "Parameter1=%~1"
Parameter 1 is "%1" and the '~' does many things (see 'CALL /?') for the
tilde operators.
Frank
>How can I pass a Regular Expression to a batch file as an
>argument if that argument contains the escape character "^" ?
>
>e.g.
>
>rebatch.bat ^hell*ld
>
>and inside the batch the RE "^hell*rld" should used with
>grep and would fetch "hello world" or "hell is the world", etc.
As Frank has said, quote the RE but on the command line.
rebatch.bat "^hell*ld"
@echo off
grep "%~1" file.txt