The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Error file Redirection maxmave Shell Programming and Scripting 3 04-23-2008 09:17 AM
awk two file redirection kamel.seg Shell Programming and Scripting 1 12-18-2007 10:17 AM
Unix command redirection swap007 Filesystems, Disks and Memory 1 10-06-2007 04:52 AM
.forward file for mail redirection giannicello UNIX for Dummies Questions & Answers 3 01-12-2002 07:06 AM
File redirection namtab UNIX for Dummies Questions & Answers 4 01-10-2002 07:48 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2008
Registered User
 

Join Date: May 2008
Posts: 16
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-09-2008 at 09:43 PM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-09-2008
Registered User
 

Join Date: Feb 2007
Posts: 15
I think it is not possible If any one knows then please reply
Reply With Quote
  #3 (permalink)  
Old 05-10-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,490
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
Reply With Quote
  #4 (permalink)  
Old 05-10-2008
Registered User
 

Join Date: May 2008
Posts: 16
Quote:
Originally Posted by matrixmadhan View Post
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?
Reply With Quote
  #5 (permalink)  
Old 05-10-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,490
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
Reply With Quote
  #6 (permalink)  
Old 05-10-2008
Registered User
 

Join Date: May 2008
Posts: 16
Quote:
Originally Posted by matrixmadhan View Post
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.
Reply With Quote
  #7 (permalink)  
Old 05-10-2008
drl's Avatar
drl drl is online now
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 497
Hi.
Quote:
Originally Posted by userix View Post
...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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0