Operating System - Linux
1752790 Members
6107 Online
108789 Solutions
New Discussion юеВ

Re: replace data in a stanza

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

replace data in a stanza

Hi all,

I want to search a file for a sting then change the last field in the last row of that stanza if it doesn't match a string:

this is how the stanza's look:


usw:
shells = /bin/sh,/bin/bsh,/bin/csh,/bin/ksh
maxlogins = 3000
logintimeout = 60
auth_type = STD_AUTH

next stanza: shells =
maxlogins =
logintimeout =
auth_type = STD_AUTH

in this example when the string usw is matched, check the auth_type and replace STD_AUTH with PAM_AUTH.

I have an idea how to do this by removing the stanza completely and then appending the usw stanza to the bottom of the file and changing STD_AUTH with PAM_AUTH.

I am sure there is a more logical way of doing this?

Thanks

Chris.
hello
4 REPLIES 4
Steven E. Protter
Exalted Contributor
Solution

Re: replace data in a stanza

Shalom Chris,

I think sed may be an answer.

websitename=bsg21.org
sed s/hpuxconsulting.com/$websitename/g hpuxconsulting.com.virt.conf > $websitename.virt.conf

What this sed sequence does is take a template file hpuxconsulting.com.virt.conf and make a changed copy in this case bsg21.org, the last one I used it on.

It efficiently does a replace and creates a new file with everywhere it sad hpuxconsulting.com now saying bsg21.org

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: replace data in a stanza

Hi Chris:

Treat each stanza as its own record:

# perl -pe '$/="";if (/^usw/) {s/STD_AUTH/PAM_AUTH/}' file

...of course to update in-place:

# perl -pi.old -e '$/="";if (/^usw/) {s/STD_AUTH/PAM_AUTH/}' file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: replace data in a stanza

Hi (again) Chris:

Oh, if you prefer 'awk' this works analogously ...

# awk 'BEGIN{RS=""};/^usw/ {sub("STD_AUTH","PAM_AUTH")};{print $0,"\n"}' file

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: replace data in a stanza

Thanks for you help again .....

I have used the awk version however the perl and sed would have done the trick.

Chris

:-)
hello