(Resend with Alex's proper address)

On 05/26/10 03:17 PM, Jack Schwartz wrote:
Hi Roland.

Thanks for helping...

On 05/26/10 02:15 PM, Roland Mainz wrote:
On Wed, May 26, 2010 at 9:59 PM, Alexander Eremin<[email protected]>  wrote:
I'm not a nawk expert, but I'll take you up on the
challenge.  I've got
this so far:

/usr/bin/nawk '$1 ~ /ramdisk/ {print $2}'
</etc/mnttab

but I'm not sure how to do the regular expression for
$2 being ^/$.  Doing

/usr/bin/nawk '$1 ~ /ramdisk/, $2 ~ /^\/$/  {print
$2}'</etc/mnttab

doesn't work.  It matches anything where $2 has a /
anywhere.  Any hints?

For example
/usr/bin/nawk '$1 ~ /ramdisk/ {if ($2 ~ /^\/$/) print $2}'</etc/mnttab
Thanks...
... example below:
-- snip --
cat<<EOF>'zzz'
/dev/dsk/c1t0d0s0       /       ufs     
rw,intr,largefiles,logging,xattr,onerror=panic,dev=800000       1273684894
/devices        /devices        devfs   dev=6400000     1273684882
/dev    /dev    dev     dev=6440000     1273684882
ctfs    /system/contract        ctfs    dev=6480001     1273684882
proc    /proc   proc    dev=64c0000     1273684882
mnttab  /etc/mnttab     mntfs   dev=6500001     1273684882
swap    /etc/svc/volatile       tmpfs   xattr,dev=6540001       1273684882
objfs   /system/object  objfs   dev=6580001     1273684882
sharefs /etc/dfs/sharetab       sharefs dev=65c0001     1273684882
/platform/sun4u-us3/lib/libc_psr/libc_psr_hwcap1.so.1   
/platform/sun4u-us3/lib/libc_psr.so.1   lofs    dev=800000      1273684890
/platform/sun4u-us3/lib/sparcv9/libc_psr/libc_psr_hwcap1.so.1   
/platform/sun4u-us3/lib/sparcv9/libc_psr.so.1   lofs    dev=800000      
1273684890
fd      /dev/fd fd      rw,dev=6700001  1273684894
swap    /tmp    tmpfs   xattr,dev=6540002       1273684895
swap    /var/run        tmpfs   xattr,dev=6540003       1273684895
-hosts  /net    autofs  nosuid,indirect,ignore,nobrowse,dev=67c0001     
1273684909
auto_home       /home   autofs  indirect,ignore,nobrowse,dev=67c0002    
1273684909
/export/home/test001    /home/test001   lofs    dev=800000      1273685096
/export/home/fleyta     /home/fleyta    lofs    dev=800000      1274771727
jupiterb48:/export/home/gisburn /home/gisburn   nfs     xattr,dev=6780003       
1274807526
/devices/ramdisk:a      /       ufs
rw,intr,largefiles,logging,xattr,onerror=panic,dev=1180001 1274892503
/dev/dsk/c1t0d0s0       /       ufs     
rw,intr,largefiles,logging,xattr,onerror=panic,dev=800000       1273684894
/devices        /devices        devfs   dev=6400000     1273684882
/dev    /dev    dev     dev=6440000     1273684882
ctfs    /system/contract        ctfs    dev=6480001     1273684882
proc    /proc   proc    dev=64c0000     1273684882
mnttab  /etc/mnttab     mntfs   dev=6500001     1273684882
swap    /etc/svc/volatile       tmpfs   xattr,dev=6540001       1273684882
objfs   /system/object  objfs   dev=6580001     1273684882
sharefs /etc/dfs/sharetab       sharefs dev=65c0001     1273684882
/platform/sun4u-us3/lib/libc_psr/libc_psr_hwcap1.so.1   
/platform/sun4u-us3/lib/libc_psr.so.1   lofs    dev=800000      1273684890
/platform/sun4u-us3/lib/sparcv9/libc_psr/libc_psr_hwcap1.so.1   
/platform/sun4u-us3/lib/sparcv9/libc_psr.so.1   lofs    dev=800000      
1273684890
fd      /dev/fd fd      rw,dev=6700001  1273684894
swap    /tmp    tmpfs   xattr,dev=6540002       1273684895
swap    /var/run        tmpfs   xattr,dev=6540003       1273684895
-hosts  /net    autofs  nosuid,indirect,ignore,nobrowse,dev=67c0001     
1273684909
auto_home       /home   autofs  indirect,ignore,nobrowse,dev=67c0002    
1273684909
/export/home/test001    /home/test001   lofs    dev=800000      1273685096
/export/home/fleyta     /home/fleyta    lofs    dev=800000      1274771727
jupiterb48:/export/home/gisburn /home/gisburn   nfs     xattr,dev=6780003       
1274807526
EOF

# 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
----

Bye,
Roland
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:
/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.

... {if ($2 == "/var/pkg") print $3} ...

works.

Is there an easy way of evaluating $VARPKG in a step prior to nawk getting executed?

I could just substitute the string in, or revert back to using grep, but I'm curious what the solution is here.

    Thanks,
    Jack


_______________________________________________
caiman-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/caiman-discuss

Reply via email to