Operating System - HP-UX
1823956 Members
3907 Online
109667 Solutions
New Discussion юеВ

Re: Standard change to a text file script

 
SOLVED
Go to solution
Michael Campbell
Trusted Contributor

Standard change to a text file script

Hi

I need to make a change to a config file on about 90 clients.
Ideally, I would like to run a script to do this rather than plodding around to each client individually.

The layout of the file is as follows:

.WORLD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = ))
(CONNECT_DATA = (SID = ))
)

The file may have 10 or 15 of these paragraphs, including other entries with the same hostnames and port numbers but the db_name in each paragraph is unique.

If is equal to ABCD, then I want to change to XYZ and to 1234.

Also if the entry does not already exist in the config file, I need to create it.

Does anyone have any ideas on this?

Any Help Appreciated

Michael
7 REPLIES 7
Slawomir Gora
Honored Contributor

Re: Standard change to a text file script

Hi,

you can try this - it generate new file


#!/bin/sh

DBNAME=$1
FILE=$2
HOSTNAME=$3
PORT=$4
FILEOUT=$FILE.new


if [ "$1" = "" ]
then
echo "Usage `basename $0` db_name file_to_modify hostname port "
exit
fi

if [ `grep -c $1 $2` -ge 1 ]
then
echo "Generating file $FILEOUT"
echo "hostname to $HOSTNAME"
echo "port to $PORT"
CMD1="s//$HOSTNAME/g"
CMD2="s//$PORT/g"
cat $FILE | sed -e "$CMD1" | sed -e $CMD2 > $FILEOUT
else
echo "adding new antry"
if [ -f $FILEOUT ]
then
echo "File $FILEOUT exist - remove it first !!!"
else
echo "Generating file $FILEOUT"
echo "$DBNAME.WORLD = " > $FILEOUT
echo "(DESCRIPTION = " >> $FILEOUT
echo "(ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = )) " >> $FILEOUT
echo "(CONNECT_DATA = (SID = )) " >> $FILEOUT
echo ") " >> $FILEOUT
fi
fi
Eric Antunes
Honored Contributor

Re: Standard change to a text file script

Hi,

You can do this in just one a shared file and go to each client and set the TNS_ADMIN registry variable:

Start->Run->Regedit->HKEY_LOCAL_MACHINE->SOFTWARE->ORACLE->New String Value->TNS_ADMIN=\...\file.ora
Each and every day is a good day to learn.
Rita C Workman
Honored Contributor

Re: Standard change to a text file script

Michael,

Exactly what platform are your clients running ??
...Windows....

??
Rita
Muthukumar_5
Honored Contributor

Re: Standard change to a text file script

I have attempted to make your requirement as,

#!/usr/bin/ksh
set -x
hostname="test"
port=20
INPUT_FILE="yourfile here"

# create an empty file as /tmp/scriptfile
>| /tmp/scriptfile

while read line; do

if [[ "$line" = "ADCD.WORLD" ]]
then

if [[ $(echo $line | grep 'HOST' | grep -q 'PORT') -eq 0 ]]
then

let text=$(echo $line | sed -e 's/HOST = [a-zA-Z]*/HOST = $hostname/;s/PORT = [0-9]*/PORT = $port/')

fi

let text=$line

fi

echo $text >> /tmp/scriptfile

done < $INPUT_FILE

And more,

are entries with string and numbers or only that pattern only.

I have tried as normal hostname , port number and databasename there.


"Also if the entry does not already exist in the config file, I need to create it."

Which you want to create it more???

It will be good to enhance the script more.
Easy to suggest when don't know about the problem!
Michael Campbell
Trusted Contributor

Re: Standard change to a text file script

Hi

Thanks for your replies.

Rita, the clients are Win2K but apparently we have a product here which can rollout out things to Win clients using UNIX scripts (especially awk).

Antunes, thanks, but i'm afraid this would still mean a trip to each PC.

Any awk hawks out there?

Michael
Eric Antunes
Honored Contributor

Re: Standard change to a text file script

Yes Michael, but only once. After that, any change you want, you do it just on the shared file.

Regards,

Antunes
Each and every day is a good day to learn.
Rodney Hills
Honored Contributor
Solution

Re: Standard change to a text file script

If you want some flexibility in your text processing script, then perl might be the ticket.

In the following program set variable %chg to your mapping. Then when a db_name is found it can use the info to update the other fields.

This script allows updating of multiple db_name.WORLD entries.

It also keeps track if an entry has been found and if not a new entry will be created at the end of the text file

HTH

-- Rod Hills
%chg=("ABCD",["XYZ",1234,0],
"EFGH",["UVW",4321,0],
"IJKL",["RST",2111,0]
);
while(<>) {
if (/^(\S+)\.WORLD=/) {
$chgflg=$chg{$1};
} elsif (/^(ADDRESS/) {
if ($chgflg) {
$host=$chgflg->[0];
$port=$chgflg->[1];
$chgflg->[2]=1;
s/HOST\s+=\s+([^)]+)/HOST = $host/;
s/PORT\s+=\s+([^)]+)/PORT = $port/;
}
}
print $_;
}
foreach $chgkey (keys %chg) {
next if $chg{$chgkey}[2];
print <$chgkey.WORLD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = $chg{$chgkey}[0])(PORT = $chg{$chgkey}[1]))
(CONNECT_DATA = (SID = $chgkey))
)
EOD
}
There be dragons...