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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
redirect output to a file name csecnarf UNIX for Dummies Questions & Answers 7 06-16-2008 04:45 AM
redirect output to log file kalyanraj Shell Programming and Scripting 2 06-07-2007 01:40 AM
redirect output to file xadamz23 Shell Programming and Scripting 4 06-29-2006 12:20 PM
How to redirect debug statement to file? redlotus72 UNIX for Dummies Questions & Answers 1 08-02-2005 09:26 PM
redirect output to file? slackware Shell Programming and Scripting 1 09-12-2003 03:57 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-27-2009
Muga801 Muga801 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 4
Redirect output of print statement to file

I have a ksh script which gathers data from a file. I need to find a way to direct the output to a new file. The code looks something like this:


Code:
 
DUP_FILE=`touch /export/.../.../dup_social_$1`
 
while (($# > 0 ))
do
# Make sure the file exists
if [[ ! -f $1 ]]
then
print "$1: does not exist or not accessible \n"
else
#open the file for input
exec 0<$1
while read LINE
do
# Store Social Security numbers to SOCIAL1 and SOCIAL2
SOCIAL1=`echo "$LINE" | awk '{ print substr ($0, 105, 9) }'`
SOCIAL2=`echo "$LINE" | awk '{ print substr ($0, 212, 9) }'`
if [ "$SOCIAL1" = "$SOCIAL2" ] ; then
print "$LINE" >> ${DUP_FILE}
fi
...
...

The output is sent to the screen, but not to $DUP_FILE. What am I missing to get the output to go to $DUP_FILE?

Last edited by vidyadhar85; 08-27-2009 at 04:20 PM.. Reason: code tag added
  #2 (permalink)  
Old 08-27-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400

Code:
DUP_FILE=`touch /export/.../.../dup_social_$1`

this won't work....
and there is no need to touch a file use redirection operator directly it will create the file itself
  #3 (permalink)  
Old 08-27-2009
Muga801 Muga801 is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 4
Thanks for the reply. I solved this problem with the following:


Code:
DUP_FILE="/export/.../.../dup_social_$1"
 
while (($# > 0 ))
do
# Make sure the file exists
if [[ ! -f $1 ]]
then
print "$1: does not exist or not accessible \n"
else
#open the file for input
exec 3>$DUP_FILE
exec 0<$1
while read LINE
do
# Store Social Security numbers to SOCIAL1 and SOCIAL2
SOCIAL1=`echo "$LINE" | awk '{ print substr ($0, 105, 9) }'`
SOCIAL2=`echo "$LINE" | awk '{ print substr ($0, 212, 9) }'`
if [ "$SOCIAL1" = "$SOCIAL2" ] ; then
print -u3 "$LINE" 
fi
...
...
# Close the open file descriptor 3
exec 3>&-


Last edited by vgersh99; 08-27-2009 at 05:23 PM.. Reason: code tags, PLEASE!
  #4 (permalink)  
Old 08-27-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
Quote:
DUP_FILE=`touch /export/.../.../dup_social_$1`
The value of ${DUP_FILE} will be blank.

Try:


Code:
DUP_FILE="/export/.../.../dup_social_$1"



---------- Post updated at 09:32 PM ---------- Previous update was at 09:25 PM ----------

Quote:
exec 3>$DUP_FILE
exec 0<$1
exec 3>&-
Unless you are experimenting with re-assigning channels this just makes the script complicated.
  #5 (permalink)  
Old 08-27-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,130
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0