How to update the contents in a file conditionally?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to update the contents in a file conditionally?
# 1  
Old 10-13-2005
How to update the contents in a file conditionally?

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
# 2  
Old 10-13-2005
Try this:

Code:
#!/usr/bin/ksh

while read country count; do
   ### try to login to oracle and set some error condition
   if [ #the failure condition is set ]; then
      count=$(($count+1))
   fi
   echo $country $count >> your_file.tmp
done < your_file
mv your_file.tmp your_file

Some of the script is not real code, because I am not sure how you are going to check if you can connect to the database.
# 3  
Old 10-13-2005
It will just append a line, what I want is I have to update that record only.

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

Thanks for your immediate responce.

Raju
# 4  
Old 10-13-2005
There are many ways to do this.

One way to do it is to write a temporary file in a directory each time a login fails. For example, your directory might look like:

india.pid1
us.pid2
uk.pid3

As you can see you can append the process id to the origins to avoid filename collisions.

Then, in a cron job, you can read the file names and add them up for each origin. Then, read the flat file where the previous values are stored into a array and add the new and old values, and write the new file.

Make sure to clean up the directory of temporary files.

You can also do this without an intermediate temporary file used as an IPC, in a single process, but if you have multiple login failures at one time, you will have have to implement file locking, which might be slower and more complex than a simple IPC like a temporary file.

There are myriad ways to do this. I suggest you use an intermediate approach like I suggested to avoid problems if you get multiple login failures in bursts - having said that, you did not describe the software architecture where the actually login failure flag (detection) occurs.
# 5  
Old 10-13-2005
Quote:
Originally Posted by rajus19
It will just append a line, what I want is I have to update that record only.

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

Thanks for your immediate responce.

Raju
Observe the code closely. I am writing to a temporary file and moving that file on to the original. i.e. If you fail to login to India, then I write 'India 2' to the temp file. Then I try to login to US. If that succeeds, then I will be writing 'US 0' (the line that I read) to the file. After the while loop is complete the temp file has the correct information about the login failures. So I move the temp file onto your original file.
# 6  
Old 10-14-2005
Thank you very much.

Regards,
Raju
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update value in field of out file using contents of another Ask

In the out.txt below I am trying to use awk to update the contents of $9.. If $9 contains a + or - then $8 of out.txt is used as a key to lookup in $2 of file. When a match ( there will always be one) is found the $3 value of that file is used to update $9 of out.txt separated by a :. So the... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Lookup in another file and conditionally modify it inline

Hi, I have an issue where i need to lookup in a given transaction file and if the same transaction is found in another file, then i need to replace a few columns with some other value. Finally, the changed and unchanged lines must be printed and stored in the same source file. for example... (5 Replies)
Discussion started by: mansoorcfc
5 Replies

3. Shell Programming and Scripting

awk match to update contents of file

I am trying to match $1 in file1 with $2 in file2. If a match is found then $3 and $4 of file2 are copied to file1. Both files are tab-delimeted and I am getting a syntax error and would also like to update file1 in-place without creating a new file, but am not sure how. Thank you :). file1 ... (19 Replies)
Discussion started by: cmccabe
19 Replies

4. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

6. Shell Programming and Scripting

How to replace a text in a file conditionally?

I have got about 100 ascii files and I want replace some variable with a new one on an HP-UX system. But I want to put a line of comments before the change. I want to make file1 to file2. I am explaining below. file1: line1 line2 export QNAME=ABC line4 line5 file2: line1 line2 #... (3 Replies)
Discussion started by: asutoshch
3 Replies

7. Shell Programming and Scripting

Loop through file to sum conditionally

Hi, I have a file with header, detail and trailer records. HDR|111 DTL|abc|100|xyz DTL|abc|50|xyz TRL|150 I need to add the values in 3rd field from DTL records. Using awk, I am doing it as follows: awk -F'|' '$1=="DTL"{a += $3} END {print a}' <source_file> However, I want to... (3 Replies)
Discussion started by: delta21
3 Replies

8. Shell Programming and Scripting

Remove file conditionally between two server using sftp

Hi, I am having 2 servers, Need to delete files from server1 if those files exist in server2 other wise no action using sftp .And the process is non-interactive way. I have got confused how to check the condition in sftp because there is non of the shell condition or loop command is executing.... (2 Replies)
Discussion started by: posix
2 Replies

9. Shell Programming and Scripting

perl - reading from a file conditionally

Hi, I am new to perl. I want to read from a file on the basis of some conditions.. I want to define parameters in a configuration file in such a manner like... etc.. in my perl script, theer is a variable like this.. then i want to read values from first if block from the file... (1 Reply)
Discussion started by: shellwell
1 Replies

10. Shell Programming and Scripting

update file contents using shell script

Hi, I am having a file which contains as below Names(aaaa ,bbbb ,cccc ,dddd) now i want the file to be updated with new value 'eeee' as below Names(aaaa ,bbbb ,cccc ,dddd ,eeee) Is there a way to script this ? Thanks, (5 Replies)
Discussion started by: drams
5 Replies
Login or Register to Ask a Question