Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gnuwin32 grep and RegEx in batchfiles

131 views
Skip to first unread message

Thomas Steinbach

unread,
Nov 27, 2009, 10:38:47 AM11/27/09
to
Hello,

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

foxidrive

unread,
Nov 27, 2009, 10:55:51 AM11/27/09
to

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


01MDM

unread,
Nov 27, 2009, 11:29:48 AM11/27/09
to
@echo off
setlocal

set regexp="^Hello"
echo 123 Hello | grep -e %regexp% 1>file.txt
echo Hello 123 | grep -e %regexp% 1>>file.txt

Thomas Steinbach

unread,
Nov 29, 2009, 2:35:34 PM11/29/09
to
Hello Foxi,

> 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

Thomas Steinbach

unread,
Nov 29, 2009, 2:40:56 PM11/29/09
to
Hello,

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

contrex

unread,
Nov 29, 2009, 3:07:13 PM11/29/09
to
On 29 Nov, 19:35, "Thomas Steinbach" <steinb...@gmx-topmail.de> wrote:

> 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'

Thomas Steinbach

unread,
Nov 29, 2009, 5:55:47 PM11/29/09
to
Hello contrex,

>> [...]


>> 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

Frank P. Westlake

unread,
Nov 29, 2009, 7:11:36 PM11/29/09
to
"Thomas Steinbach" news:hev0un$5qf$00$1...@news.t-online.com...

> 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


foxidrive

unread,
Nov 29, 2009, 10:18:54 PM11/29/09
to
On Sun, 29 Nov 2009 23:55:47 +0100, "Thomas Steinbach"
<stei...@gmx-topmail.de> wrote:

>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


0 new messages