String containing % on the command line.

R

Roedy Green

I wrote a little REGEX utilty that takes a regex and a collection of
files and searches them for the pattern, outputting its findings as a
CSV file.

The catch is the % character. Even inside quotes, this character seems
to be reserved by the command processor. How can I sneak the %
character in a string as a parameter on the command line past the
command line parser?
 
R

Roedy Green

The catch is the % character. Even inside quotes, this character seems
to be reserved by the command processor. How can I sneak the %
character in a string as a parameter on the command line past the
command line parser?

By experiment I discovered a solution:

If you are using Vista, and your regex contains a %
character, you must double it to sneak it past the command
processor which normally treats % as meaning macro
substitution, even inside quotes.

Now the other question is what you do write for \" inside a regex on
the Vista command line?

What about Linux command processors?
 
A

Andreas Leitgeb

Roedy Green said:
What about Linux command processors?

Usually, you enclose hairy arguments in single quotes, which
protects everything, except single quotes, of course :)

$ echo '%$%&%%&%/%/$/"`´/%%&/%%/'
%$%&%%&%/%/$/"`´/%%&/%%/

Fortunately, you can also easily concatenate differently
quoted parts of one argument, so you can do like that:

$ foo=42
$ echo '%$%&$/%$ value of $foo is '"'$foo'"' /&/&)%&()%$&$'
%$%&$/%$ value of $foo is '42' /&/&)%&()%$&$

The single quotes inside the double quotes are taken verbatim.

and finally with respect to single single quotes:

$ echo '%$&/%$ now follows a single quote: -->'\''<-- here!'
%$&/%$ now follows a single quote: -->'<-- here!

Some shells (csh) still barf on the exclamation mark,
even in single quotes, but I consider them either
misconfigured or broken.
 
R

Roedy Green

Now the other question is what you do write for \" inside a regex on
the Vista command line?

If you are using Vista, and your parm contains a ", you
must encode it with \" so that the command processor won't
treat it as the end of parameter. The command processor will
strip of the \ and pass it to the program without it.
 
T

Tim Slattery

Roedy Green said:
Now the other question is what you do write for \" inside a regex on
the Vista command line?

I haven't tested it, but I suspect the Vista command line is
interpreting the backslash as an escape, much as "system.Out.print"
does. So it grabs the next character and tries to figure out what the
combination means. So try doubling the backslash, that'll probably do
it.
What about Linux command processors?

I haven't used Linux, so I can't be of help.
 
A

Arne Vajhøj

Bernie said:
And for the record, there are no "Linux command processors". There are
Windows shells and non-Windows shells. The non-Windows group breaks into
Bourne compatible and csh compatible. That's about it.

Only if you consider non-Windows to mean *nix flavors.

There are certainly platforms that are not Windows and not have
sh/csh/ksh/tcsh/bash !

Arne
 
A

Andrea Francia

Lew said:
Enclose the dicey part in single quotes.

The bash shell supports $'\n' and similar $'' constructs for specific
tricky characters. 'man bash' tells all - there's on online man
somewhere, or do it from cygwin.

A very useful guide for bash is the "Bash Advanced Programming Howto".

By the way if you are invoking the command line from Java (e.g. using
Runtime.exec()) the bash is not involved in the process.
 
A

Arne Vajhøj

Andrea said:
A very useful guide for bash is the "Bash Advanced Programming Howto".

By the way if you are invoking the command line from Java (e.g. using
Runtime.exec()) the bash is not involved in the process.

Unless he needs to use shell as executable because shell
functionality is needed (like internal commands or redirection).

Arne
 
R

Roedy Green

Unless he needs to use shell as executable because shell
functionality is needed (like internal commands or redirection).

for example if he wants to use any of the commands internal to the
command processor that don't have corresponding independent
executables such as cp rm ls mv cat.

In that case you must spawn bash or other shell, and feed it commands
as parameters.
 
R

Roedy Green

I read somewhere that files like .BAT, .COM, .SCR, .EXE, .CMD are all
natively executable for windows.

Hmm. I presume though you need the explicit .bat,where you can leave
it off for a command processor?

Do you know if this works in anything prior to XP?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top