Visit Our UNIX and Linux User Community


Redirect output of print statement to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect output of print statement to file
# 1  
Old 08-27-2009
Redirect output of print statement to file

Smilie 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  
Old 08-27-2009
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  
Old 08-27-2009
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  
Old 08-27-2009
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
# 5  
Old 08-27-2009
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #139
Difficulty: Easy
The early Unix environment and the client-server program networking model were key elements in the development of the Internet.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect the output of the telnet to a file

Hi, I am using cygwin. Below is my script that reads all ip ports for iplist.txt and telnets to it. ( file="iplist.txt" while read line do echo $line echo $(telnet $line) done <"$file" ) > output2.txt ~ while the output2.txt gets the first echo but does not show the second... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

3. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

4. Shell Programming and Scripting

How to redirect output of ls to a file?

Hi All, I want to redirect only the file names to a new file from the ls -ltr directroy. how Can i do it. my ls -ltr output will be as below. -rwxr-xr-x 1 118 103 28295 Jul 26 2006 event.podl -rwxr-xr-x 1 118 103 28295 Jul 26 2006 xyz.podl I want my new file... (6 Replies)
Discussion started by: girish.raos
6 Replies

5. Shell Programming and Scripting

How redirect standard output to a file

Hi guys, i have a script named purgeErrors.ksh, when i execute this script i need to redirect the output to a log file in the same directory, how can i do that ?? -- Aditya (5 Replies)
Discussion started by: chaditya
5 Replies

6. Shell Programming and Scripting

Redirect the output to a .xls file

Hi, I am using the sybase database server though unix platform and try to retrieve some data by the help of select statement, and my main issue is i want to redirect all the the data i.e. retrieve from the select statement to a .xls file with the same column name and the outputs. How can i do... (5 Replies)
Discussion started by: anupdas
5 Replies

7. UNIX for Dummies Questions & Answers

redirect output to a file name

Hi all!! is possible to assign the output of some command to filename, i.e. grep_output.txt Otherwise, I want to open a new file which name is inside another, how can I do it? Thanks a lot! (7 Replies)
Discussion started by: csecnarf
7 Replies

8. UNIX for Dummies Questions & Answers

Redirect output to a file

Ahhhrrrggg I'm having a brain fart... I want to take the output of a command and redirect it to a file... This works.... $ man cp | cat >> copy_help but this doesn't keytool -help |cat >> keytool_help It just produces... these lines... more keytool_help ] ... ... (11 Replies)
Discussion started by: jimmyc
11 Replies

9. UNIX for Dummies Questions & Answers

How to redirect debug statement to file?

Hi, I need to redirect the debug statements to out.txt {(set -x) >> out.txt} ??? (1 Reply)
Discussion started by: redlotus72
1 Replies

10. Shell Programming and Scripting

redirect output to file?

Hi: I am currently working on a program which requires direct its ouput to a file here is an example ./proram arg_1 arg_2 when program ends all output will be arg_2 file Is that possible I am not a bad programmer, However I am stuck there. Can anyone give a hint? Thanks SW (1 Reply)
Discussion started by: slackware
1 Replies

Featured Tech Videos