Operating System - HP-UX
1847817 Members
5475 Online
104021 Solutions
New Discussion

Problem replacing text in a script

 
SOLVED
Go to solution
Gus Mestousis
Frequent Advisor

Problem replacing text in a script

I am trying to write a script that gets the value of the logival volume an application is on and then adds an r after the volume group so that I can run fsadm on it.
Somthing along the lines of cat /etc/fstab |grep $APPHOME |awk '{ print $1 }' sed ?????

How would I insert the "r" into /dev/vg01/lvol6, sometimes clients use different naming conventions ... /dev/vg02/appname

Thanks!
Sure, let me just drop everything and work on your problem.
10 REPLIES 10
Sridhar Bhaskarla
Honored Contributor

Re: Problem replacing text in a script

I would do the following quickly -

....
...
for ENTRY in $(grep $APPHOME /etc/fstab|awk '{print $1}')
do
LVOL=$(echo $ENTRY|awk '{FS="/";print $4}')
RLVOL=$(echo $ENTRY|sed 's/'$LVOL'/r'$LVOL'/')
echo $RLVOL
done

-Sri


You may be disappointed if you fail, but you are doomed if you don't try
Rajeev  Shukla
Honored Contributor

Re: Problem replacing text in a script

Other simple one would be
LVOL=`cat /etc/fstab|grep $APPHOME|awk -F / '{print $4}'`
VG=`cat /etc/fstab|grep $APPHOME|awk -F / '{print $3}'`

And when you want to substitute with 'r' you can do it

RLVOL=/dev/${VG}/r${LVOL}

cheers
Rajeev
john korterman
Honored Contributor

Re: Problem replacing text in a script

Hi Gus,

you could use the korn shell's built-in functions for string handling, e.g.:

#!/usr/bin/sh

APPHOME=$1

cat /etc/fstab | grep $APPHOME | while read line
do
LVOL=`echo $line | awk '{print $1}'`
ENDING=`basename $LVOL`
BEGINNING="${LVOL%%$ENDING}"
echo ${BEGINNING}r${ENDING}
done
~
regards,
John K.
it would be nice if you always got a second chance
Robin Wakefield
Honored Contributor
Solution

Re: Problem replacing text in a script

Hi Gus,

grep $APPHOME /etc/fstab | awk '{print $1}' | sed 's+.*/+&r+'

Rgds, Robin
F. X. de Montgolfier
Valued Contributor

Re: Problem replacing text in a script

Hi,

bdf gives you the name of the lv your application is using.

I prefer using bdf to grepping it from fstab, as it will give you the logical volume the application is installed on even if it is sharing its logical volume...

#[SAVE]> bdf /tmp/test/
Filesystem kbytes used avail %used Mounted on
/dev/vg00/lvol4 1024000 40785 921815 4% /tmp

The following command should, (though there might be tyops: I don't have access to my system fron here), give you the correct answer:

bdf $APPHOME |grep /dev/ |sed 's!(\^[^ ]+/\)!\1r!'|awk '{print $1}'

The bdf part will give you the logical volume on which your application resides, the grep will ensure that only the relevant line is printed, the sed will find the longest string containing a '/' since the begining of the line, and add 'r'after it, and the awk will print only the first argument...

I won't promise that the command is _fast_, though ;-)

Cheers,

Fran??ois-Xavier
H.Merijn Brand (procura
Honored Contributor

Re: Problem replacing text in a script

a5:/ 108 # grep -v nfs /etc/fstab
/dev/vg00/lvol3 / vxfs delaylog 0 1
/dev/vg00/lvol1 /stand hfs defaults 0 1
/dev/vg00/tmp /tmp vxfs rw,suid,largefiles,delaylog,datainlog 0 2
/dev/vg00/lvol7 /usr vxfs delaylog 0 2
/dev/vg00/var /var vxfs delaylog 0 2
/dev/vg00/opt /opt vxfs delaylog 0 2
/dev/vg00/home /home vxfs delaylog 0 2
/dev/vg00/u /u vxfs rw,suid,largefiles,delaylog,datainlog 0 2
/dev/vg00/pro /pro vxfs rw,suid,largefiles,delaylog,datainlog 0 2
/dev/vg00/data /data vxfs rw,suid,largefiles,delaylog,datainlog 0 2
/dev/vg00/wrk /wrk vxfs rw,suid,largefiles,delaylog,datainlog 0 2
/dev/cd0 /cdrom cdfs ro,rr,noauto 0 0
a5:/ 109 # echo $APPHOME
/pro
a5:/ 110 # perl -nle'/$ENV{APPHOME}/&&s:\s.*::&&s:vg\d+/:$&r:&&print' /etc/fstab
/dev/vg00/rpro
a5:/ 111 #
Enjoy, Have FUN! H.Merijn
F. X. de Montgolfier
Valued Contributor

Re: Problem replacing text in a script

Sorry, following up on myself...

I don't know why I insisted on using awk when sed is enough for the job...
Let's try doing it right this time:

bdf $APPHOME |grep /dev/ |sed 's!(\^[^ ]+/\)(\[^ ]+\).+!\1r\2!'

- ^[^ ]+/ should give you the basename of your logical volume,
- [^ ]+ should give you the logical volume name,
- the .+ is there to engulf all the rest of the line

Please remember that this is untested, as I don't have access to my platform. It should be near the mark, though...

Cheers,

Fran??ois-Xavier
Gus Mestousis
Frequent Advisor

Re: Problem replacing text in a script

Thanks for all the great replies.

Enjoy your holidays!

Gus

Sure, let me just drop everything and work on your problem.
Steven E. Protter
Exalted Contributor

Re: Problem replacing text in a script

Attached is a perl script that handles this based on creating filelists. Be careful using it on /etc files,but it works, is easy to modify and understand.

It actually uses sed btw.

Steve
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
H.Merijn Brand (procura
Honored Contributor

Re: Problem replacing text in a script

Ahhhhhhrgggggg, someone still using perl4!

Never use this, not even as an example.
And *if* you need using perl4, at least add -w to the command line.

When converting this to perl5, start with something like

#!/usr/local/bin/perl

use strict;
use warnings;
use File::Find;
use Sys::Hostname;

:
:
:
Enjoy, Have FUN! H.Merijn