echo command and file I/O Redirection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting echo command and file I/O Redirection
# 1  
Old 05-10-2008
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:

Code:
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:

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

Last edited by userix; 05-10-2008 at 01:43 AM..
# 2  
Old 05-10-2008
I think it is not possible If any one knows then please reply
SmilieSmilie
# 3  
Old 05-10-2008
Quote:
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
Code:
-T, --temporary-directory=DIR
              use DIR for temporaries, not $TMPDIR or /tmp;  multiple  options
              specify multiple directories

# 4  
Old 05-10-2008
Quote:
Originally Posted by matrixmadhan
Why do you feel creating a temp file is a constraint ?
even sort uses temp files
check out the option -T
Code:
-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?
# 5  
Old 05-10-2008
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
# 6  
Old 05-10-2008
Quote:
Originally Posted by matrixmadhan
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.
# 7  
Old 05-10-2008
Hi.
Quote:
Originally Posted by userix
...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 ...
Have you looked at all the options on the man page for sort? ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command output redirection to file issues

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)
Discussion started by: ananan
7 Replies

2. UNIX for Dummies Questions & Answers

Redirection of file input to command

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)
Discussion started by: pseudo
6 Replies

3. UNIX and Linux Applications

output redirection command

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)
Discussion started by: knowledgeSeeker
2 Replies

4. UNIX for Dummies Questions & Answers

echo and redirection

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)
Discussion started by: animesharma
4 Replies

5. Shell Programming and Scripting

Display and write in file in one echo command

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)
Discussion started by: vivek1489
1 Replies

6. Shell Programming and Scripting

Help with tar --append command output redirection

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)
Discussion started by: pganguly46
0 Replies

7. Shell Programming and Scripting

redirection if command help please

#!/bin/bash clear echo "Hello $USER" echo "Do you wish to run this network configuration script ?" read option if($option == 'Y' || 'y' ) then echo " auto eth0 iface eth0 inet static address 192.168.1.161 ... (5 Replies)
Discussion started by: wezzyb
5 Replies

8. Shell Programming and Scripting

problem with suppressed output to file using echo and tee command

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)
Discussion started by: shahanali
1 Replies

9. Shell Programming and Scripting

nohup command o/p redirection

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)
Discussion started by: ammu
3 Replies

10. Filesystems, Disks and Memory

Unix command redirection

Hi all,, Is there any way to redirect the command o/p directaly to a memory location instead of redirecting it to the file?? (1 Reply)
Discussion started by: swap007
1 Replies
Login or Register to Ask a Question