[SOLVED] Using GNU grep in a Windows batch script?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

[SOLVED] Using GNU grep in a Windows batch script?

#1 Post by Shohreh » 26 Feb 2020 08:10

Hello,

The following works with when using GNU grep on Linux…

Code: Select all

joe# grep -shoP '<div class="legend"><a href="/en/map/.+?</a>' input.txt
… but doesn't when used in a Windows batck file:

Code: Select all

C:\>grep -shoP '<div class="legend"><a href="/en/map/.+?</a>' input.txt
< was unexpected at this time.

C:\>grep -shoP ^"<div class=\"legend\"><a href=\"/en/map/.+?</a>" input.txt
< was unexpected at this time.

C:\>grep -shoP "<div class=\"legend\"><a href=\"/en/map/.+?</a>" input.txt
The system cannot find the file specified.

C:\>grep -shoP "<div class=\"legend\"><a href=\"/en/map/.+?</a>" . input.txt
The system cannot find the file specified.
Any idea what must be done to get Windows and/or grep to work?

Thank you.
Last edited by Shohreh on 27 Feb 2020 05:30, edited 1 time in total.

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

Re: Using GNU grep in a Windows batch script?

#2 Post by dbenham » 26 Feb 2020 10:08

Your problem is grep and cmd.exe have different quoting and escape rules. You need to account for both, remembering that cmd.exe quoting/escaping is processed before grep.

cmd.exe does not have a mechanism to escape a double quote within a double quoted string. Once quoting has begun, the next quote always turns quoting off. This is important because you have lots of < and > characters that must either be escaped as ^< and ^>, or else quoted.

I believe the following works

Code: Select all

grep -shoP "<div class=\"legend\"><a href=\"/en/map/.+?^</a^>^" input.txt
Using color coding, the blue text is quoted from a cmd.exe perspective, and the red is unquoted:

grep -shoP "<div class=\"legend\"><a href=\"/en/map/.+?^</a^>^" input.txt

The easiest solution is probably to represent the quote literals using \x22. Then you can simply enclose the entire regex in quotes and not worry about any cmd.exe escapes.

Code: Select all

grep -shoP "<div class=\x22legend\x22><a href=\x22/en/map/.+?</a>" input.txt

Dave Benham

Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Re: Using GNU grep in a Windows batch script?

#3 Post by Shohreh » 26 Feb 2020 14:34

Perfect! Thank you very much.

Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Re: Using GNU grep in a Windows batch script?

#4 Post by Shohreh » 26 Feb 2020 18:34

I have another issue with cmd.exe.

I used ssed.exe ("Super sed") because the original is always greedy.

I'm trying to remove all the lines except those that start with "#" (ie. keep the comments), but no matter what I try, I can't get it to work:

I tried doubling or even tripling each circumflex, and using its hex ASCII number, to no avail:

Code: Select all

ssed.exe -R "s@^[^#]+$@@g" input.txt > output.txt
ssed.exe -R "s@^^[^^#]+$@@g" input.txt > output.txt
ssed.exe -R "s@^^^[^^^#]+$@@g" input.txt > output.txt
ssed.exe -R "s@\x5e[\x5e#]+$@@g" input.txt > output.txt
Any idea?

Thank you.

ShadowThief
Expert
Posts: 950
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Using GNU grep in a Windows batch script?

#5 Post by ShadowThief » 26 Feb 2020 19:20

If you're already using grep, couldn't you just

Code: Select all

grep ^^# input.txt >output.txt
?
Last edited by ShadowThief on 27 Feb 2020 16:27, edited 1 time in total.

siberia-man
Posts: 153
Joined: 26 Dec 2013 09:28
Contact:

Re: Using GNU grep in a Windows batch script?

#6 Post by siberia-man » 27 Feb 2020 03:31

print the lines starting with #

as sed:

Code: Select all

sed -n "/^#/p" FILENAME
as grep:

Code: Select all

grep "^#" FILENAME

Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Re: Using GNU grep in a Windows batch script?

#7 Post by Shohreh » 27 Feb 2020 05:30

Brilliant!

I'll use ssed.exe so I wont have to provide two binaries for the same job.

Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Re: [SOLVED] Using GNU grep in a Windows batch script?

#8 Post by Shohreh » 11 Mar 2020 08:14

For some reason, the following code works OK when calling it with either "*.gpx" or a specific filename with no spaces in (inputfile.gpx), but breaks if it holds spaces. The problem seems to come from %%f in the replacement passed to ssed:

Code: Select all

REM OK when called with my.bat "*.gpx" or my.bat inputfile.gpx
REM NOK when called with my.bat "my input file.gpx"
for %%f in ("%1") DO echo Handling %%f & ssed.exe -R "s@<name>.+?</name>@<name>%%f</name>@g" < "%%f" > "%%f.NAME"
Output:

Code: Select all

C:\>sed.name.replace.with.name.bat "my input file.gpx"
Handling "my input file.gpx"
ssed.exe: -e expression #1, char 32: unterminated `s' command
I've tried adding double-quotes around %%f, or replacing the double-quotes with single quotes, still no go.

Thank you.

siberia-man
Posts: 153
Joined: 26 Dec 2013 09:28
Contact:

Re: [SOLVED] Using GNU grep in a Windows batch script?

#9 Post by siberia-man » 11 Mar 2020 09:03

Replace this one:

Code: Select all

for %%f in ("%1") DO ... <name>%%f</name>@g" < "%%f" > "%%f.NAME"
with:

Code: Select all

for %%f in ("%~1") DO ... <name>%%~f</name>@g" < "%%~f" > "%%~f.NAME"

Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Re: [SOLVED] Using GNU grep in a Windows batch script?

#10 Post by Shohreh » 11 Mar 2020 09:10

Thanks for the tip!

Post Reply