Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Piping a command containing an odd number of double_quotes?

May
12,834
163
I want to do command1 | command2. It fails when command1 contains an odd number of double_quotes because the pipe character is seen as being quoted. Here's an overly simple example.

Code:
v:\> echo "" | findstr /r .
""

v:\> echo """ | findstr /r .
""" | findstr /r .

In real life, I want to pipe the output of echo %@function[], so I have no control over what's echoed, and I can't setdos /x-7 because that will turn off the special meaning of [ and ].

How do I work around this?
 
... Put command1 in a batch file?
OK, but it seems a bit like cheating. :-)pstat

I wanted to count the double_quotes in a process's command line. This is what I had in mind (@PSTAT is a plugin).

Code:
echo %@pstat[15880,c] | grep -o \" | wc -l

The grep -o pipe would put each occurrence of double_quote on a line of it's own (bare); wc would count the lines.

The wc pipe will ALWAYS fail because it's seen as quoted. And the grep pipe would fail (same reason) if the echoed string has an odd number of quotes. It seems there should be an easy way to do this sort of thing ... a way to terminate a command without having to match an odd double_quote.

Notes:

(Command grouping) does not override this odd_number_of_quotes behavior. And I couldn't get ^q to help.

SETDOS /X-7 doesn't help because it also turns off [ and ].

Using @FIELDS["^"",%@pstat[15880,c]] doesn't work because the string contains back_quotes and that screws up " being seen as a separator.

Using a BTM,

Code:
v:\> type qpipe.btm
echo %@pstat[15880,c] | grep -o \"

v:\> qpipe.btm | wc -l
1402

I can get the same result using Gnu's TR.EXE to change double_quotes to newlines and then counting the lines. That suffers similarly. The tr command (tr "\"" "\n") can't be piped because it contains an odd number of double_quotes.

The command line itself is rather long 7637 characters. It's the command line for "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\ServiceHub\Hosts\ServiceHub.Host.netfx.x86\ServiceHub.IdentityHost.exe", which is run automatically when I run DevEnv. It's not a very interesting command line. I could post it if anyone wan't to see it.
 
P.S., I didn't try hard enough to use ^q. This works (but would fail if the string echoed had an odd number of double_quotes).

Code:
v:\> echo %@pstat[8800,c] | grep -o \^q | wc -l
1402
 
I think, rather than piping, I would redirect the command to a temp file, then sic grep/wc/whatever on the file. Avoid the parser's complexities altogether.
 
I also discovered (re-discovered?) @COUNT, which I can make work. But I have to be careful about the first time through the parser. This fails,

Code:
v:\> echo %@count[^q,"""]
TCC: Syntax error "@count[^q,"""]"

while this works

Code:
v:\> set zz="""

v:\> echo %zz
"""

v:\> echo %@count[^q,%zz]
3
 
Can you replace the quotes with an uncommon character and then count that?
 
Can you replace the quotes with an uncommon character and then count that?
Yes, but there's still behavior that I can't make sense of. For example, this works

Code:
v:\> echo %@count[x,%@replace[",x,"""]]
3

while this doesn't

Code:
v:\> set zz="""

v:\> echo %zz
"""

v:\> echo %@count[x,%@replace[",x,%zz]]
TCC: Syntax error "@count[x,%@replace[",x,%zz]]"

The only difference between the two examples is the change of """ to %zz (which equals """). It boils down (somehow) to even/odd number of double_quotes (even inside a function).
 

Similar threads

Back
Top