1752801 Members
5748 Online
108789 Solutions
New Discussion юеВ

Need help on AWK Script

 

Need help on AWK Script

Hi,

Can some one help me.

I have ouput as below:

/celcomdb17/oradata/Celcom/CDR16.dbf
/celcomdb17/oradata/Celcom/INDEX_32K/32K_CCOLL_COUNTERIND44.dbf

I would like to truncate/chop the last field.

The output shout be

/celcomdb17/oradata/Celcom
/celcomdb17/oradata/Celcom

Please assist.

TQ
9 REPLIES 9
Steven E. Protter
Exalted Contributor

Re: Need help on AWK Script

Shalom,


$ cat RMAN_policy.sh
#! /bin/ksh
#
# set command line variables
#
PATH=/usr/openv/netbackup/bin:/usr/openv/netbackup/bin/admincmd:/usr/openv/netbackup/bin/goodies:/usr/openv/netbackup/bin/goodies/support:/usr/openv/volmgr/bin:/usr/openv/volmgr/bin/goodies:/opt/VRTSvxfs/sbin:$PATH
#USAGE="USAGE: RMAN_policy.sh "


#shift `expr $OPTIND - 1`

#INSTANCE=$1
#SERVER=$2
#PRIORITY=$3
#VPOOL=$4
#BPATH=$5
#OSV=$6

#PASSES=`wc -l $1 | awk '{ print $1 }'`
#PASSNO=0

function while_read_LINE
{
cat $1 | while read LINE
do

echo "$LINE"

INSTANCE=`echo "$LINE" | awk '{ print $1 }'`
SERVER=`echo "$LINE" | awk '{ print $2 }'`
PRIORITY=`echo "$LINE" | awk '{ print $3 }'`
VPOOL=`echo "$LINE" | awk '{ print $4 }'`
BPATH=`echo "$LINE" | awk '{ print $5 }'`
OSV=`echo "$LINE" | awk '{ print $6 }'`

echo "$INSTANCE"
echo "$SERVER"
echo "$PRIORITY"
echo "$VPOOL"
echo "$BPATH"
echo "$OSV"

echo bppolicynew ora_"$INSTANCE"
echo bpplinfo ora_"$INSTANCE" -set -ut -active -blkincr 0 -collect_tir_info 0 -compress 0 -crossmp 1 -disaster 0 -encrypt 0 -follownfs 0 -multiple_streams 1 -policyjobs 0 -pool "$VPOOL" -priority "$PRIORITY" -pt Standard -residence foo -chkpt 1 -chkpt_intrvl 30
echo bpplsched ora_"$INSTANCE" -add Full
echo bpplschedrep ora_"$INSTANCE" Full -cal 0 -freq 86400 -mpxmax 1 -rl 5 -st FULL
echo bpplinclude ora_"$INSTANCE" -add "$BPATH"/*
echo bpplclients ora_"$INSTANCE" -add "$SERVER" Solaris "$OSV"

#PASSNO=`expr $PASSNO + 1`

#echo END PASS "$PASSNO"

#INSTANCE=$INSTANCE; shift 6
#SERVER=$SERVER; shift 6
#PRIORITY=$PRIORITY; shift 6
#VPOOL=$VPOOL; shift 6
#BPATH=$BPATH; shift 6
#OSV=$OSV; shift 6
done
}
$

From:
http://www.unix.com/shell-programming-scripting/85239-parsing-file-line.html

Or:

http://cdunix.blogspot.com/2008/09/parsing-connectdirect-stats-part-2_26.html

awk is great for breaking up strings using field separators.
/ is a special character, requires a little more work.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Need help on AWK Script

Hi:

> I would like to truncate/chop the last field

# awk -F/ '{OFS="/";print $1,$2,$3}' file

...would yield the output you requested.

Regards!

...JRF...
VK2COT
Honored Contributor

Re: Need help on AWK Script

Hello,

I am not sure I undersxtand what you want.

If you truncate the last "field" from

/celcomdb17/oradata/Celcom/INDEX_32K/32K_CCOLL_COUNTERIND44.dbf

... it should be:

/celcomdb17/oradata/Celcom/INDEX_32K

... not as you said in the original message.

So, if you really want the last
"slash-separated" field to be removed,
it is as simple as:

for i in /celcomdb17/oradata/Celcom/CDR16.dbf
/celcomdb17/oradata/Celcom/INDEX_32K/32K_CCOLL_COUNTERIND44.dbf
do
dirname $i
done

The result is:

/celcomdb17/oradata/Celcom
/celcomdb17/oradata/Celcom/INDEX_32K

Please clarify your requirements.

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Steven E. Protter
Exalted Contributor

Re: Need help on AWK Script

Shalom,

Second try:

[root@bersheva ~]# F1="/steve/direname1/file.dbf"
[root@bersheva ~]# d1=$(echo $F1 | awk -F "/" '{ print $1}')
[root@bersheva ~]# echo $d1

[root@bersheva ~]# d1=$(echo $F1 | awk -F "/" '{ print $1 }')
[root@bersheva ~]# echo $d1

[root@bersheva ~]# d1=$(echo $F1 | awk -F "/" '{ print $2 }')
[root@bersheva ~]# echo $d1

You can go forward from here.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com

Re: Need help on AWK Script

TQ for the reply.

Appology my typo.

Actually output should be :

/celcomdb17/oradata/Celcom
/celcomdb17/oradata/Celcom/INDEX_32K

The OFS "/" length is varried.
I would like to get only directory.

Thanks
-Kamarul
bright image
Frequent Advisor

Re: Need help on AWK Script

If you only want the directory use VK2COT's method above.

See "man dirname" for more information.
James R. Ferguson
Acclaimed Contributor

Re: Need help on AWK Script

Hi (again):

> I would like to get only directory

Well, that makes it clear, now.

Use the 'dirname' function as VK2COT suggested, or skip spawning this process and let the shell do all the work:

# cat mylist
/celcomdb17/oradata/Celcom/CDR16.dbf
/celcomdb17/oradata/Celcom/INDEX_32K/32K_CCOLL_COUNTERIND44.dbf

# while read DIR
do
echo ${DIR%/*}
done < mylist

/celcomdb17/oradata/Celcom
/celcomdb17/oradata/Celcom/INDEX_32K

Regards!

...JRF...


john korterman
Honored Contributor

Re: Need help on AWK Script

Hi,

based on Mr. Ferguson's first suggestion:

$ awk -F/ '{OFS="/"; $NF="";print substr($0,1,length($0)-1)}' < yourinputfile


regards,
John K.
it would be nice if you always got a second chance
Arturo Galbiati
Esteemed Contributor

Re: Need help on AWK Script

Hi TQ,
dirname is the command for you:
dirname /celcomdb17/oradata/Celcom/CDR16.dbf
/celcomdb17/oradata/Celcom/

HTH,
Art