On 05/26/10 03:38 PM, Roland Mainz wrote:
On Thu, May 27, 2010 at 12:28 AM, Roland Mainz<[email protected]> wrote:
On Thu, May 27, 2010 at 12:17 AM, Jack Schwartz
<[email protected]> wrote:
Thanks for helping...
[snip]
# read file into variable "input"
input="$(< 'zzz' )"
# pick the data, \3 refers to the content matched by the pattern in
the 3rd ()-bracket pair
match="${input/~(Elr-g).*([^[:space:]]*ramdisk[^[:space:]]*)([[:space:]]+)([^[:space:]])([[:space:]]+).*/\3}"
if [[ $match != "$input" ]] ; then
printf 'match is %q\n' "$match"
else
print -u2 'no match found.'
fi
-- snip --
The core in this example is the line...
-- snip --
${input/~(Elr-g).*([^[:space:]]*ramdisk[^[:space:]]*)([[:space:]]+)([^[:space:]])([[:space:]]+).*/\3}
-- snip --
It grabs the value of variable "input" and returns the content which
matches the extended regular pattern (that does the ~(Elr-g) specify:
Extended regular pattern ("E") with left ("l") and right ("r")anchors
and greedy matching ("-g", the greedy mode matches the shortest
pattern, the default for regular expressions is to match the longest
character sequence)) in the 3rd bracket pair. If no match is found the
full value of variable "input" is returned.
Notes:
1.I made the example a bit more verbose, AFAIK the [:space:] can be
replaced with a plain white space assuming /etc/mnttab doesn't use
both SPACE and TAB in the same file.
2. [^ ] means: This pattern matches a single character which is not a
space (^ means "not")
3. * matches zero or more characters and + matches one or more characters
----
[snip]
This info is good to know. However, for this particular example, I think it
is clearer to use nawk just because it will be shorter. Alex helped me with
this already, and it works:
Erm... I doubt it is shorter because you run a seperate process which
shuffels around a few MB during loading&&starting. It only _looks_
shorter (but you are right, for those who don't do regex pattern stuff
often the solution using "awk" looks easier (but IMO at an expense of
being an overkill. IMHO heavywheight filters like sed/awk/tr/etc.
should only be used if there are more data to process than at least
1MB. It's IMO not efficient to move around flies using elephants...)).
/usr/bin/nawk '$1 ~ /ramdisk/ {if ($2 ~ /^\/$/) print $2}' \
</etc/mnttab
However, I am running into a problem when I try to do something similar, but
where I pass the match-string in as a variable:
VARPKG=/var/pkg
varpkg_fstype=($/usr/bin/nawk \
'{if ($2 == $VARPKG) print $3}'</etc/mnttab
Problem is that $VARPKG is not getting interpretted into /var/pkg before
getting passed to nawk.
You use '...' to pass the awk program and anything within '...' is
passed as plain literal to awk. If you want that $ gets interpreted by
the shell then you must use "..."-style string literals.
Erm... I forgot something. If you use some kind of parameters in "awk"
scripts it is recommended to pass them as _arguments_ and extract the
values using ARGV[i]. See nawk(1). Otherwise you may hit some
interesting issues with quoting when your parameter value contains
characters which may be interpretable as "awk" program fragment.
----
Hmmm... I wonder if I hit them...
Actually, I put $VARPKG in double-quotes but it didn't match when it
should have.
$VARPKG doesn't work (causes nawk to err out)
"$VARPKG" doesn't match
/var/pkg doesn't match
"/var/pkg" is the only one of these which matches.
I'll explore ARGV later tonight. Thanks for the tips!
Thanks,
Jack
Bye,
Roland
_______________________________________________
caiman-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/caiman-discuss