Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying text file

Status
Not open for further replies.

Johnsf

MIS
Apr 5, 2004
45
NZ
Hi,

Is there a way on Linux where by I can set it up to automatically open a text file every night and add a line of text right at the top of the file?

I have a file which contains data and I need to add a header to it each night.

Cheers,

John
 
add an entry to the crontab :
Code:
crontab -e

and then add this line :

Code:
* 23 * * * mv abc abc.old ; echo "my header" > abc ; cat abc.old >> abc ; rm abc.old

Then write and quit the crontab. ( :wq )

where abc is your filename. This will execute at 11pm each night.

One way anyway ...



--------------------------------------------------
Free Database Connection Pooling Software
 
typic, sedj, very quick + (more)dirty
what if abc is NOT there?
where are you (pwd) ?
good: you use vi
do you know :x ?
 
Another way of doing this is create a file containing only your header. Then use cat as so:
Code:
 cat myheaderfile mydatafile > mynewdatafile

Then you can check it in the morning, and rename as needed.

Or you can just
Code:
 cat myheaderfile mydatafile > mydatafile
if you are a confident sort...

This also has the advantage you can use a script to create the header file, use a static header, or create it yourself daily.


What this does and why..

the cat command is designed to read one or more files specified on the command line and send the results to standard out. The original use was to combine 2 files (concatenate) but it normally gets used nowadays to print files to the screen.

HTH

:)
Xaq

---

"I'm just here to regulate funkyness"
 
Careful here. Trying this:

cat myheaderfile mydatafile > mydatafile

on Solaris complains that mydatafile and mydatafile are identical and only ends up overwriting mydatafile with my headerfile, losing the mydatafile data. Does this happen with linux too? I'd suggest a test before you implement it in anger. HTH.
 
That's the way it's supposed to work. I'd stick with sedj's method.

">" overwrites the destination, ">>" appends to it.
 
I know. Hence the warning. An addition to sedj's response might be to check for the existence of the file first, then do a conditional depending on the result.
 
Thanks for that guys.... got it working :)

Cheers,

John
 
KenCunningham:
Sorry, My bad.
Code:
 Cat file1 file2 > file2
won't work. I thought I had it working... Must be lack of caffine...

---

"I'm just here to regulate funkyness"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top