awk - help in removing some text

Hey Guys,

Earlier I asked a question under Solaris, which I got great help... thanks.

Although I got the script working for what it really needed to do, I am looking for a bit of help to change the output for nicer reading.

my script gets a list of zones under a global-zone and puts this to a text file e.g
/newpool/server1
/testpool/server2

###  get the zone admin list
zoneadm list -iv | awk '{ print $4 }' | awk ' length>3 ' | grep -v "PATH" | sort >> $ZONES

in a while loop I output the zones and then show, under root, what reboots have been set up (to see if any have been hashed out)

in my loop I echo the zone appending another text file

while read ZONE
do
    echo "$ZONE"  >> $MSG
    echo "--------------------------------------" >> $MSG
    cat $ZONE/root/var/spool/cron/crontabs/root | grep "/shutdown" >> $MSG
    echo "\n" >> $MSG
done < "$ZONES"

I want to change the output of echo "$ZONE >> $MSG to remove "/pooltype/" and only echo out the server name.

I have tried adding grep - "//" or "\/\\/" but this works but either does not show any server ("//") or shows the server with pool ("\/\\/")

But I am thinking AWK might be better for what I am trying to do but I can't find anything with awk on how to do this, can anyone suggest how to do this?

I have never used sed so don't know anything about that.

thanks in advance

davy

You are doing this on Solaris? Are you using /bin/sh , /bin/ksh or /bin/bash ?
I assume $ZONE contains, eg, /newpool/server1 and you want it to appear as server1 .
In /bin/sh , as it looks like a filepath, use

echo `basename $ZONE` >> $MSG

or even

basename $ZONE >> $MSG

For ksh and bash, try

echo ${ZONE##*/} >> $MSG

Andrew

1 Like

Thanks apmcd47 your absolutely correct, it is in Solaris, and i have the script as /bin/sh

I tried your first option of

echo `basename $ZONE` >> $MSG

which worked

coming from windows support not long started my first unix admin job, so learning lots quickly...

thanks a million..

davy

How about doing all of this in one single awk script only? The following is for showing the concept only; not having access to a solaris system, I can't test my proposal.

means: get field 4 from the zoneadm list if its longer than three chars and does not contain the "PATH" string. Doesn't

zoneadm list -iv | awk '(length($4) > 3) &&( $4 !~ "PATH") {print $4)'

do the same?

means: for all zones found above print the lines containing "/shutdown" from the crontab files for all the zones found.
Doesn't

zoneadm list -iv | 
awk '
(length($4) > 3) && 
($4 !~ "PATH")  {CRP = $4 "/root/var/spool/cron/crontabs/root"
                 while (1 == getline LN < CRP)  if (LN ~ /\/shutdown/)  {print $4
                                                                         print "---------"
                                                                         print LN
                                                                        }
                 close (CRP)
                }
' > $MSG

do the same?

On a Solaris/SunOS system, you'll probably need to change:

zoneadm list -iv | awk '(length($4) > 3) && ($4 !~ "PATH") {print $4)'

to:

zoneadm list -iv | nawk '(length($4) > 3) && ($4 !~ "PATH") {print $4)'

or:

zoneadm list -iv | /usr/xpg4/bin/awk '(length($4) > 3) && ($4 !~ "PATH") {print $4)'

You will also need to use the same change from awk to nawk or /usr/xpg4/bin/awk in the longer script RudiC suggested.

1 Like