"Kev" <kreid@cis.strath.ac.uk> writes:
> Very good, that works!
>
> Could you define a "globbing pattern"? Is it the ksh equivalent of a
> regexp?
>
Shells glob. Utilities like sed, grep and awk use regular expressions.
When you use the shell to do
echo abc*
it "globs" - expanding to all files matching the pattern.
In general, the concepts are
* = zero or more characters
? = single character
. = .
[a-z] = matches characters a-z
A regular expression is very different but on the surface it seems similar.
[a-z] = matches characters a-z - that's the same.
But the others are different
. = any single character.
* = zero or more copies of the character set BEFORE it.
so [a-z]* can have two different meanings
As a regular expression, it will match zero or more lower case letters.
As a glob - it will match any word that starts with a lower case letter.
if you used the shell's
echo [a-z]*
it would print all files starting with a lower case letter.
if you did a
grep '[a-z]*' <file
it would match all lines with zero or more lower case letters.
So it would match ALL lines. All have zero or more letters.
Regular expressions also has "anchors" such as '^'
grep '^[a-z]*' <file
will print all lines that START with zero or more lower case letters.
grep '^[a-z][a-z]*' <file
will print all lines that start with ONE or more lower case letters.
Another confusing difference is that file globbing uses '?' to match
any single character, and '.' is not special.
Yet with regular expressions '.' matches any character.
echo *.*
will list all files that have a dot in the name.
The regular expression to print all lines with a dot would be
grep '\.' <file
As I said - the '.' is special. You need the backslash to tell the
regular expression engine that you want to "escape" the special
meaning.
If you used
grep '.' <file
you would print all files that contain one or more characters.
For more info, see
http://www.grymoire.com/Unix
and
http://www.grymoire.com/Unix/Regular.html
Some of these tutorials are old, as I wrote them 10 years ago.
> Interestingly, I have another regular expression in my script, and this
> seems to work fine:
> *[!0-9]*
>
> As I say, I am new to ksh!
>
>
> Stephane Chazelas wrote:
>
>> On 17 Aug 2006 01:55:11 -0700, Kev wrote:
>> > Hi,
>> >
>> > In a ks script I have written, the first arg is an IP address - I
>> > decided to check the validity of this using a regular expression:
>> >
>> > #Check validity of arg1
>> > if [[ "$1" =
>> > \b(?
?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
>> > ]]
>> > then
>> > echo "Arg1 is a valid IP address"
>> > else
>> > echo "Arg1 is invalid"
>> > fi
>> >
>> > The regex may not be the most effiecient, but it is right, anyway, when
>> > I run the script, I get:
>> >
>> > ./scriptabc.ksh: syntax error at line 17 : `(' unexpected
>> >
>> > What am I doing wrong? (I am relatively new to ksh)
>> [...]
>>
>> You're using a perl regular expression where ksh is expecting a
>> ksh globbing pattern.
>>
>> try:
>>
>> if perl -e 'exit(1) unless shift(@ARGV) =~ /your-regexp/' "$1"
>> then
>> ...
>>
>>
>> instead
>>
>>
>> --
>> Stephane
>
--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.