Home » Infrastructure » Unix » How to update the contents in a file conditionally?
How to update the contents in a file conditionally? [message #142087] Thu, 13 October 2005 07:33 Go to next message
rajus19
Messages: 18
Registered: September 2005
Junior Member
Hi All,

I have a data file which has two columns Location and the Count.

The file looks like this
India 1
US 0
UK 2
China 0

What I have to do is whenever I fails to login to Oracle then I have to add 1 to the count for that location.

Whenever my script fails to login to Oracle for a particular location, the script needs to find the record for that location and then change the count. I should not change the count for all the locations, instead I need to change the count only for that location.

Ex: Suppose if I am trying to login to Oracle for India location and fails to login then I have to update the count to 2.

Your help will be greatly appreciated.

Thanks in Advance
Raju

[Updated on: Thu, 13 October 2005 07:35]

Report message to a moderator

Re: How to update the contents in a file conditionally? [message #142130 is a reply to message #142087] Thu, 13 October 2005 09:26 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10708
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
>>Suppose if I am trying to login to Oracle for India location
How would you identify it?
through tnsnames.ora?
Like
scott/tiger@UK ?
scott/tiger@India?
Re: How to update the contents in a file conditionally? [message #142132 is a reply to message #142130] Thu, 13 October 2005 09:43 Go to previous messageGo to next message
rajus19
Messages: 18
Registered: September 2005
Junior Member
yes exactly.

I uses a for loop in which I everytime it takes one location at a time and calls a function which will login to oracle, if it fails at that time I have to update the count in a file(status file) only for that location.

And one more thing is how to get the count for a particular location.


Thanks for your immediate response.

Raju

[Updated on: Thu, 13 October 2005 10:09]

Report message to a moderator

Re: How to update the contents in a file conditionally? [message #142180 is a reply to message #142132] Thu, 13 October 2005 12:56 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10708
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
#
# dblog is the name of the script
# I am executing it twice here.
oracle@mutation#dblog
-----------------------------------------------------
------------This is before the loop --
-- Each entry in this listing is in tnsnames.ora --
-- None except mutation and lawd is a valid entry --
-- a new file is generated at the end --
-- All counts are updated except valid ones --
-----------------------------------------------------
India 1
Canada 2
USA 3
mutation 4
UK 5
lawd 6
China 7
------------New file --------------------------------
------------end of file --------------------------------
India 2
Canada 3
USA 4
mutation 4
UK 6
lawd 6
China 8

#
# Execute again.
#
oracle@mutation#dblog
-----------------------------------------------------
------------This is before the loop --
-- Each entry in this listing is in tnsnames.ora --
-- None except mutation and lawd is a valid entry --
-- a new file is generated at the end --
-- All counts are updated except valid ones --
-----------------------------------------------------
India 2
Canada 3
USA 4
mutation 4
UK 6
lawd 6
China 8
------------New file --------------------------------
------------end of file --------------------------------
India 3
Canada 4
USA 5
mutation 4
UK 7
lawd 6
China 9
#
#This is the script
#Have your valid tnsentries (as in tnsnames.ora) in a file /tmp/dblist
#The script reads the file everytime and creates a new one.
#
oracle@mutation#cat dblog
#!/usr/bin/bash
echo -----------------------------------------------------
echo ------------This is before the loop                 --
echo -- Each entry in this listing is in tnsnames.ora    --
echo -- None except mutation and lawd  is a valid entry  --
echo -- a new file is generated at the end               --
echo -- All counts are updated except valid ones         --
echo -----------------------------------------------------
cat /tmp/dblist
echo ------------New file --------------------------------
export ORACLE_HOME=/u01/app/oracle/product/9.2.0
cat /tmp/dblist | awk '{print $1}' | while read name
do
err=`sqlplus -s system/manager@$name <<EOF
set echo off
set head off
set feed off
WHENEVER SQLERROR EXIT SQL.SQLCODE
prompt
prompt
exit;
EOF`
if echo $err | grep "ERROR" > /tmp/junk1
then
   awk -F" " '$1 ~/'$name'/ {print  $1" "$2+1 }' /tmp/dblist >> /tmp/dblist.final
else
   awk -F" " '$1 ~/'$name'/ {print  $1" "$2 }' /tmp/dblist   >> /tmp/dblist.final
fi
done
echo ------------end of  file --------------------------------
mv /tmp/dblist.final /tmp/dblist
cat /tmp/dblist

[Updated on: Thu, 13 October 2005 13:05]

Report message to a moderator

Re: How to update the contents in a file conditionally? [message #142315 is a reply to message #142180] Fri, 14 October 2005 02:25 Go to previous message
rajus19
Messages: 18
Registered: September 2005
Junior Member
Thank you very much Mahesh to your prompt response, of course its your greatness.

Regards,
Raju
Previous Topic: DISPLAY setting for Oracle 9.2.0.6.0 on SunOs 5.8
Next Topic: unable to execute sqlldr with ftp in single shell scripts
Goto Forum:
  


Current Time: Mon Nov 25 19:56:16 CST 2024