1834312 Members
2690 Online
110066 Solutions
New Discussion

cron question

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

cron question

I'm going to setup a cron job that creates a large file. I need to insert a line of text that looks like this "" as the second line in this file.

How can I do this from a command line?
8 REPLIES 8
Olivier Masse
Honored Contributor

Re: cron question

Mmmmm if I understand correctly, you don't want to create a second tempfile for space reasons but insert directly this line in. This is not possible. Even if you go more low-level than the usual tools you'll just be able to append to the file or overwrite parts of it.

If you really cannot write in a tempfile, the only solution is to leave space in your file upon creation to hold this second line. You can leave a long empty string at line 2, ended by a newline. Then you can open it, goto line 2 and replace it with your string, padded to have the same length. I don't think any shell tool can pull this off easily, so I suggest you try perl. :)

If you can write to another file, the simplest way I can think of is using awk like this:

cat original_file | awk '{
print;
print "";
while (getline)
{
print;
}
}' > new_file
mv new_file original_file

I don't have a prompt at hand so there might be a syntax error or two.

Good luck
Vibhor Kumar Agarwal
Esteemed Contributor

Re: cron question

Okay, lets try.
Can't give a full solution but maybe some points might click.

Since cron will be writing to that file, you can't do anything with that otherwise your cron will stop.

So use "fuser" to find out whether at that moment that file is being used. If not append the line after the first one.

It can be done by sed but at the moment i don't remember the syntax.

So something like
if fuser filename
then
sed ...
fi
Vibhor Kumar Agarwal
Michael Schulte zur Sur
Honored Contributor

Re: cron question

Hi,

what about this? zz is old and zz1 is new file.

greetings,

Michael

#!/bin/ksh -xu
typeset -i10 LC=`wc -l zz|awk '{print $1}'`
let LC=LC-1
head -1 zz > zz1
print "insert line" >> zz1
tail -${LC} zz >> zz1
mv zz1 zz
Rob Johnson_3
Regular Advisor

Re: cron question

Will this make any difference? I found out I can insert the text at the beggining of the file. Will this make it easier?




Kasper Hedensted
Trusted Contributor

Re: cron question

Perhaps you could alter the cron job, so that you start by creating a new file with this line, and then append to this file ?
Vibhor Kumar Agarwal
Esteemed Contributor
Solution

Re: cron question

I think you have given yourself a good idea.

So you can create the file as
touch exp
sed -e 's/text_to_replace/^/g'

Now start your cron job, which will add test to this file.

When your cron has completed, if you really want this to be 2nd one, add the first line using the same sed command.
Vibhor Kumar Agarwal
Michael Schulte zur Sur
Honored Contributor

Re: cron question

Hi,

this makes it a lot easier
print "insert line" > new_file
cat old_file >> new_file

Michael
Rob Johnson_3
Regular Advisor

Re: cron question

I'm not at work and don't have a unix box here at home so I'm not sure which way will work best. These responses have given me a lot of suggestions and options.

I love this forum. When I post here, I always get a solution or plenty of good ideas to solve the issue at hand!!!

Again, thank you from the guy who wishes he was a scripter!!!!!
Chuckle, chuckle, chuckle....