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:
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:
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
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?
Why do you feel creating a temp file is a constraint ?
even sort uses temp files
check out the option -T
Why do you feel creating a temp file is a constraint ?
even sort uses temp files
check out the option -T
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
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,
I have a peculiar issue w.r.t redirecting the command output to a file when using loop.
I am redirecting command output to same file in a series of if condition statements, but if one block of if condition statement writes the log to the file , the subsequent block of if condition... (7 Replies)
Hello,
I'm new to Unix (working with OS X 10.8.5) and therefore at the beginning of my adventure. If I ask something stupid, then this is not intentional, but simple nescience. :rolleyes:
I have a problem with the redirection of text file content to echo. I was experimenting with redirection... (6 Replies)
Dear All,
./waf --run scratch/myfirst > log.out 2>&1
The above is a command line to redirect the output to a file called log.out.
what is the 2>&1 part for ?
Thank you (2 Replies)
I am trying to write a simple script which prints some output, but also saves the same in a logfile.
echo hello
echo hello >> logfile
Is there anyway i Can write single sentence of code and get the same result:
( i am using CSH ) (4 Replies)
Hi All,
I want to display content on command promt and also write in file.
For that iI ahve to write two sentence
echo "XXXXXXX"
echo "XXXXXXXX" >> 1.txt
Is there any way to write in one echo statement (1 Reply)
I am using tar command to append daily database backups on tape. "tar --append " command help me to do this.
But tar --append command does not produce any output on stdout if it succeed.
I want the output for that appended command to a log file.
This log file should contain only the name of the... (0 Replies)
Hi,
When I run the following command in terminal it works. The string TEST is appended to a file silently.
echo TEST | tee -a file.txt &>/dev/null
However, when I paste this same line to a file, say shell1.sh, and use bourne shell .
I run this file in terminal, ./shell1.sh.
However I... (1 Reply)
Hi
I am using nohup command in script(say nohup ls- ltr > somefile 2>&1 & ). I dont want any kind of output to be displayed on screen. When i tried the above nohup it still gives me some out put on screen like
2991
Done >somefile 2>&1
Please you let me know what is... (3 Replies)