Page 1 of 1

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

Posted: 26 Feb 2020 08:10
by Shohreh
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.

Re: Using GNU grep in a Windows batch script?

Posted: 26 Feb 2020 10:08
by dbenham
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

Re: Using GNU grep in a Windows batch script?

Posted: 26 Feb 2020 14:34
by Shohreh
Perfect! Thank you very much.

Re: Using GNU grep in a Windows batch script?

Posted: 26 Feb 2020 18:34
by Shohreh
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.

Re: Using GNU grep in a Windows batch script?

Posted: 26 Feb 2020 19:20
by ShadowThief
If you're already using grep, couldn't you just

Code: Select all

grep ^^# input.txt >output.txt
?

Re: Using GNU grep in a Windows batch script?

Posted: 27 Feb 2020 03:31
by siberia-man
print the lines starting with #

as sed:

Code: Select all

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

Code: Select all

grep "^#" FILENAME

Re: Using GNU grep in a Windows batch script?

Posted: 27 Feb 2020 05:30
by Shohreh
Brilliant!

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

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

Posted: 11 Mar 2020 08:14
by Shohreh
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.

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

Posted: 11 Mar 2020 09:03
by siberia-man
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"

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

Posted: 11 Mar 2020 09:10
by Shohreh
Thanks for the tip!