Update the Contents of a File in Un

Is there a way to update the contents of a file (this file contains a list of sqls) without piping it to a new file using a shell script ? I have 15 processes reading from a file and I want each of them to read the latest updates made to the file so that no process runs the sqls twice ... Is this possible ?

>>>
ex or vi with a here or file script works fine, e.g.:
ex myfile < script
script:
%s/oldSQLstuff/newSqLStuff/
w
q

OR
ex myfile << EOT
%s/oldSQLstuff/newSqLStuff/
w
q
EOT

of course, backup myfile before testing.

>>>
What do you mean by "update the contents of a file"? Do you want to add to, delete from, or modify the current contents of the file? Depending on exactly what you need to do, my suggestion/recomendation would be to use Perl. For example, using Perl to duplicate Dlonra's suggestion, you'd do it in 1 line like this:
perl -pi.bak -e 's/oldSQLstuff/newSqLStuff/g' myfile

That will do an inline edit of your file as well as create a backup file.

>>>
The first solution seems to be working just fine ... Thanx a LOT !
I haven't tried the Perl solution yet .. I shall do that soon ... thanx again ... I really appreciate the inputs ...

>>>
The second solution is ok for simple changes (as in my example). If you want to do anything other than simple, ex/vi with scripts is, in my experience, much more flexible. Among other things, you can easily make "multiple passes" over the file.
Also, if you are running your conversion script from a prompt, using vi is fun.

>>>
While I do agree that there are times that ex/vi would be a better solution ("use the right tool for the job"), I think I'll take issue with 2 of the first solution statements.

>> If you want to do anything other than simple, ex/vi with scripts is, in my experience, much more flexible.

I'd disagree with that, Perl is not only more flexible, but it's also more powerful than ex/vi with scripts. There are quite a lot of things you can do with Perl that are totaly impossible to do with ex/vi, and as I've shown here, this problem can be solved with Perl in only 1 line as opposed to the 5+ lines used with the ex/vi solution.

>> Among other things, you can easily make "multiple passes" over the file.

That's something that you want to try to avoid, if at all possible. It's far more efficient to process the file in 1 pass. But, if you really want/need to, Perl can easily make multiple passes.

See Also
Unix Administrator Career Path

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.