echo command and file I/O Redirection

I have a colon-delimited text file of names, addresses and phone numbers. I am trying to write a script that can add additional entries and then sort it alphabetical by last name and resave it to the original file. I am using C shell to script. This is the section of my script that I wish to sort the data file and then save it to itself:

set file = `sort -k1,2 $myfile`
echo $file > $myfile

This code works and does sort, but the problem comes when I try to echo the sorted data back into the original file. Echo apparently does not show the original newline characters, so the updated file I get no longer is sorted by rows, but rather one continuous string. Or the problem could lie in the setting of the 'file' variable, in which newline characters are not set into the variable 'file' as well. I have tried another method using the following code:

sort -k1,2 $myfile > 0
cat 0 > $myfile
rm 0

Basically above, instead of using the echo command, I actually redirect the sorted output to a temporary file called '0' and then cat it back, overwriting the original contents of $myfile. While this method works, it requires that I temporarily create the '0' file.

My underlining question is: How can I sort the data file and then save it back into itself without having to create a temporary file '0' and still have the newline characters that separate each row remain? Thanks

I think it is not possible If any one knows then please reply
:mad::eek:

Why do you feel creating a temp file is a constraint ?
even sort uses temp files
check out the option -T

-T, --temporary-directory=DIR
              use DIR for temporaries, not $TMPDIR or /tmp;  multiple  options
              specify multiple directories

I am curious if it is possible to do it without temp file, because I can set the data into a variable, but the only problem is the newlines not being stored as well. Is this a limitation with the 'set' command, or the 'echo' command. This a CS class project I have to complete. So in the end, I have to give read and execute permissions to my professor. If I give him r+x permissions and he runs the script from my personal folder, would it not be able to create a temp file, since he does not have write permissions in my folder?

to overcome that specify the temp directory where sticky bit is set, such that the directory is writable by anybody and only owner could delete the created files

If I use sticky bit, wouldn't that not allow my script to delete the temp file when he runs it? Since he is not the owner of my script.
If I need to allow him to run my script from within my directory, would giving him read, write, and executable allow the temp file to be created and deleted when he runs the script? I want to make sure my script runs exactly as I intend it to and not glitch up because the temp file cannot be created when it comes to the point in my script where I resave the sorted data. That is why I asked the question in OP. If I can temporarily store the data correctly in a variable and then re-set it to the original file, I wouldn't have to worry about using a temp file. Thanks for your replies.

Hi.

Have you looked at all the options on the man page for sort? ... cheers, drl

userix,

with sticky bit I meant it on the directory - hope you also meant the same.

When the data can't fit in the pipe kernel data structure automatically it retorts to storage in the form of file.

Is it something, user should be able to create only process space and no files at all ?

Even with what I had suggested its going to be a silent operation, temp file creation and deletion.

I grabbed the definition of sticky bit from wikipedia: "when set, items inside the directory can be renamed or deleted only by the item's owner, the directory's owner, or the superuser"

So would this mean that when my professors runs the script, it will create the temp file with no issues, but when it comes to "rm" the temp file, he won't be able to, since sticky bit forbids deleting of any files in the set directory, thus throwing an error when it hits the "rm tempfile" part of my script? I guess in the end, I can just give him write permissions as well to the folder containing this script, that way the temp file will create and delete without errors. Thanks again.