Operating System - HP-UX
1827279 Members
2602 Online
109717 Solutions
New Discussion

Re: Regarding shell script

 
SOLVED
Go to solution
vind123
Regular Advisor

Regarding shell script

i have a file netmap.cfg in which i need to modify the details under fep entry. i had modified the conn.retry.ltattempts from 6 to 99 and finally i had added two lines
:tcp.max.time.to.wait=180:\ and :runstep.max.time.to.wait=300: User's are asking to do the modification automatically that is via unix script. I had done earlier using sed. But i am not sure how to do this one via unix shell script since there are repeated entries.
fep:\
:conn.retry.stwait=00.00.30:\
:conn.retry.stattempts=3:\
:conn.retry.ltwait=00.10.00:\
:conn.retry.ltattempts=99:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=128:\
:sess.snode.max=127:\
:sess.default=1:\
:comm.info=fep;1364:\
:comm.transport=tcp:\
:comm.bufsize=4096:\
:pacing.send.delay=0:\
:pacing.send.count=0:\
:tcp.max.time.to.wait=180:\
:runstep.max.time.to.wait=300:


Netmap.cfg original file contents.
-----------------------------------
topasw2:\
:conn.retry.stwait=00.00.30:\
:conn.retry.stattempts=3:\
:conn.retry.ltwait=00.10.00:\
:conn.retry.ltattempts=6:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=128:\
:sess.snode.max=127:\
:sess.default=1:\
:comm.info=topasw2;1364:\
:comm.transport=tcp:\
:comm.bufsize=4096:\
:pacing.send.delay=0:\
:pacing.send.count=0:


NDMATTT1:\
:conn.retry.stwait=00.00.30:\
:conn.retry.stattempts=3:\
:conn.retry.ltwait=00.10.00:\
:conn.retry.ltattempts=6:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=128:\
:sess.snode.max=127:\
:sess.default=1:\
:comm.info=NDMATTT1;1364:\
:comm.transport=tcp:\
:comm.bufsize=4096:\
:pacing.send.delay=0:\
:pacing.send.count=0:


kscadcs1:\
:conn.retry.stwait=00.00.30:\
:conn.retry.stattempts=3:\
:conn.retry.ltwait=00.10.00:\
:conn.retry.ltattempts=6:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=128:\
:sess.snode.max=127:\
:sess.default=1:\
:comm.info=kscadcs1;1364:\
:comm.transport=tcp:\
:comm.bufsize=4096:\
:pacing.send.delay=0:\
:pacing.send.count=0:


rapprod:\
:conn.retry.stwait=00.00.30:\
:conn.retry.stattempts=3:\
:conn.retry.ltwait=00.10.00:\
:conn.retry.ltattempts=6:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=128:\
:sess.snode.max=127:\
:sess.default=1:\
:comm.info=rapprod;1364:\
:comm.transport=tcp:\
:comm.bufsize=4096:\
:pacing.send.delay=0:\
:pacing.send.count=0:


ffep:\
:conn.retry.stwait=00.00.30:\
:conn.retry.stattempts=3:\
:conn.retry.ltwait=00.10.00:\
:conn.retry.ltattempts=6:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=128:\
:sess.snode.max=127:\
:sess.default=1:\
:comm.info=fep;1364:\
:comm.transport=tcp:\
:comm.bufsize=4096:\
:pacing.send.delay=0:\
:pacing.send.count=0:
11 REPLIES 11
IT_2007
Honored Contributor

Re: Regarding shell script

If you open it with vi then

:%s/string/newstring/g

which will change from string ---> new string globally in the file.
Mel Burslan
Honored Contributor
Solution

Re: Regarding shell script



cp -p netmap.cfg netmap.cfg.sav #save a backup copy just in case
cat netmap.cfg | sed -e "1,$s/ltattempts=6/ltattempts=99/" >/tmp/mytempfile
echo ":tcp.max.time.to.wait=180:\\" >> /tmp/mytempfile
echo ":runstep.max.time.to.wait=300:" >> /tmp/mytempfile
cat /tmp/mytempfile > netmap.cfg
________________________________
UNIX because I majored in cryptology...
Jonathan Fife
Honored Contributor

Re: Regarding shell script

What I think you're asking for is a method to replace only the parameters within the "fep" section and not the other sections.

Here's an awk script example:
BEGIN {fep=0}
{if ( $0 ~ /fep:/ ) { fep=1 }
if ( fep==1 && $0 ~ /:tcp.max.time.to.wait=/ )
{ printf(":tcp.max.time.to.wait=%s:\\\n",tcp); continue }
if ( fep==1 && $0 ~ /:runstep.max.time.to.wait=/ )
{ printf(":runstep.max.time.to.wait=%s:\\\n",run); continue }
if ( fep==1 && $0 ~ /:conn.retry.ltattempts=/ )
{ printf(":conn.retry.ltattempts=%s:\\\n", con); continue }
if ( fep==1 && $1 !~ /:.*/ ) fep=0;
print $0}


Save it to a file (eg. netmap_change.awk) and call it from the commandline as:
awk awk -v con=$con -v tcp=$tcp -v run=$run -f netmap_change.awk netmap.cfg

Your script will have to set the con, tcp, and run variables ahead of time to whatever the users input.
Decay is inherent in all compounded things. Strive on with diligence
James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi:

# perl -pe '$ok=1,next if /^ffep:/;if ($ok) {s/ltattempts=6:/ltattempts=99:/};if (eof) {s/$/\\/};END{print ":tcp.max.time.to.wait=180:\\\n:runstep.max.time.to.wait=300:\n"}' Netmap.cfg

...This should change the 'ltattempts' value, but only under the 'ffep' header; and add a backslash to the last line of the original file before appending the two lines you want to the end.

Regards!

...JRF...

vind123
Regular Advisor

Re: Regarding shell script

I tried the awk script that jonathan had pasted in the post.

I had set con=99, run=100,tcp=180 and i ran the below command
awk -v con=$con -v tcp=$tcp -v run=$run -f test.awk netmap.cfg

The fep entry in the netmap.cfg file got changed as below.

fep:\
:conn.retry.stwait=00.00.30:\
:conn.retry.stattempts=3:\
:conn.retry.ltwait=00.10.00:\
:conn.retry.ltattempts=99:\
:contact.name=:\
:contact.phone=:\
:descrip=:\
:sess.total=255:\
:sess.pnode.max=128:\
:sess.snode.max=127:\
:sess.default=1:\
:comm.info=fep;1364:\
:comm.transport=tcp:\
:comm.bufsize=4096:\
:pacing.send.delay=0:\
:pacing.send.count=0:

The belw two lines after last line :pacing.send.count=0: didn't get added after running the script.
:tcp.max.time.to.wait=180:\
:runstep.max.time.to.wait=300:

Also after adding these lines i need the
:pacing.send.count=0: end with a backslash
i.d :pacing.send.count=0:\

In the old netmap.cfg in fep entry these two lines tcp and runstep lines are not present. These two lines are new lines that needs to be added after the pacing.send.count line.

Also in fep entry i am able to see two space
before :conn.retry.ltattempts=99:\ had been removed after running the script.




James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi:

I suggest you reread my original post. I believe that my perl script accomodates your needs, including the backslash addition.

I would respectfully remind you that you have a good many replies to a good many questions lately to which you have not offered any points. Points add value to later readers in searching for answers that actually worked. Points also offer thanks to those who have helped along the way.

Regards!

...JRF...
Jonathan Fife
Honored Contributor

Re: Regarding shell script

Sorry, I thought the new lines were already there from the last time you added them and you just wanted them updated to be new user-entered values.

I suggest going with JRF's elegant perl solution at this point unless you have some specific reason not to. The awk script could be modified to add the new lines, but why re-do the work when JRF already did it for you?
Decay is inherent in all compounded things. Strive on with diligence
vind123
Regular Advisor

Re: Regarding shell script

Hi James,
I dont know perl. how do i run this one?
I assign the points at the end when i get answers. Is it a correct way of doing or do i need put points while discussing. I am new to this forum
James R. Ferguson
Acclaimed Contributor

Re: Regarding shell script

Hi (again):

If you paste the command line, beginning with 'perl' that I posted, you will run the Perl interpreter. My post is simply a commandline perl script.

Every recent HP-UX has Perl. You can see the version you are running by doing"

# perl -v

You can obtain the latest (5.8.8) here should you wish:

http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=PERL

You may certainly assign points at the closure of a thread when you are satisfied that you have a solution. Thank you for asking and thank you for following through. You can read about the point system here:

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
vind123
Regular Advisor

Re: Regarding shell script

James,

I tried to run it and i am getting the below error

perl -pe '$ok=1,next if /^ffep:/;if ($ok) {s/ltattempts=6:/ltattempts=99:/};if (eof) {s/$/\\/};END{print ":tcp.max.time.to.wait=180:\\\n:runstep.max.time.to.wait=300:\n"}' netmap.cfg
syntax error in file /tmp/perl-ea13388 at line 1, next 2 tokens "END{"
syntax error in file /tmp/perl-ea13388 at line 2, next token "}"
Execution of /tmp/perl-ea13388 aborted due to compilation errors.
vind123
Regular Advisor

Re: Regarding shell script

Thanks a lot for the info