Computing.Net > Forums > Unix > update the contents of a file in Un

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

update the contents of a file in Un

Reply to Message Icon

Name: vrrajeeb
Date: February 9, 2004 at 19:38:39 Pacific
OS: HP-UX 11
CPU/Ram: 1024 M
Comment:

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 ?




Sponsored Link
Ads by Google

Response Number 1
Name: Dlonra
Date: February 10, 2004 at 07:18:46 Pacific
Reply:

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.


0

Response Number 2
Name: FishMonger
Date: February 10, 2004 at 09:51:13 Pacific
Reply:

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.


0

Response Number 3
Name: vrrajeeb
Date: February 10, 2004 at 17:06:15 Pacific
Reply:

Dlonra's 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 ...


0

Response Number 4
Name: Dlonra
Date: February 10, 2004 at 17:18:16 Pacific
Reply:

fishmonger's 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.


0

Response Number 5
Name: FishMonger
Date: February 10, 2004 at 18:41:44 Pacific
Reply:

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 Dlonra's 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 allot 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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: update the contents of a file in Un

how to identify the size of a file www.computing.net/answers/unix/how-to-identify-the-size-of-a-file-/7523.html

count the occurence of a char in a string www.computing.net/answers/unix/count-the-occurence-of-a-char-in-a-string/8516.html

accessing the contents of a file www.computing.net/answers/unix/accessing-the-contents-of-a-file/6149.html