Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Confused over results using "tee" in piped command

First post here, so hopefully all guidelines are followed, and thanks in advance for any replies.

I'm working on a shell script(BASH) that processes a csv file and performs various tasks with the data. All is well, except I want to use 'tee' to send output from 'wc' to a file as well as pipe it to 'cut'. What I'm trying to accomplish is a) tally the record count from a list of files, and b) send the details of 'wc' to an output file.

This code successfully tallies the record count, but obviously isn't sending the output from 'wc' to an output file:


Code:
#!/bin/bash	
unset vCount verifyRecordCount
vCount=0
verifyRecordCount=0
   
outfile="./test2a.out"	
EXTRACT_FILES=`ls -d ./*`

for filename in ${EXTRACT_FILES}
do
   `wc -l $filename >> $outfile` 
   vCount=`wc -l $filename | cut -d" " -f1`
   echo "vCount="$vCount>>$outfile
   ((verifyRecordCount=verifyRecordCount+${vCount}))
done
echo "verifyRecordCount="$verifyRecordCount>>$outfile

I then insert a 'tee' into the mix and suddenly the setting of the vCount variable is 'broken'.

Here's the modified code:

Code:
#
# again, with feeling
#
unset vCount verifyRecordCount
vCount=0
verifyRecordCount=0
   
outfile="./test2b.out"
EXTRACT_FILES=`ls -d ./*`

for filename in ${EXTRACT_FILES}
do
   `wc -l $filename >> $outfile` 
   #vCount=`wc -l $filename | cut -d" " -f1`
   vCount=`wc -l $filename | tee >>$outfile | cut -d" " -f1`
   echo "vCount="$vCount>>$outfile
   ((verifyRecordCount=verifyRecordCount+${vCount}))
done
echo "verifyRecordCount="$verifyRecordCount>>$outfile
cat $outfile

I'd paste the results, but since this should run as is, suffice it to say that vCount is not getting set, and is blank.

What am I missing?

Thanks in advance.

Gary
Sponsored Links
    #2  
Old 02-08-2012
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,192
Thanks: 233
Thanked 1,707 Times in 1,635 Posts
You've used 'tee' without giving it any filenames, which is effectively the same as 'cat'. The one file it writes by default, stdout, you've redirected to a file, leaving nothing left for cut to get.

If you want tee to actually put the output into several files, don't redirect it, just tell which file you want


Code:
... | tee -a filename | ...

The -a is for append.

---------- Post updated at 01:12 PM ---------- Previous update was at 01:10 PM ----------

What I'd actually do, though?


Code:
wc -l < input > output
read NUMBER <output

Much more efficient, using nearly pure builtins except wc.
The Following User Says Thank You to Corona688 For This Useful Post:
jazzmusic (02-08-2012)
Sponsored Links
    #3  
Old 02-08-2012
Registered User
 

Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
doh! I did specify a filename, but used redirect rather than simply let 'tee' do what it does.

Thanks a ton, Corona. This was driving me nuts
    #4  
Old 02-08-2012
gary_w's Avatar
Registered User
 

Join Date: Oct 2010
Posts: 303
Thanks: 24
Thanked 67 Times in 62 Posts
Try this:


Code:
vCount=`wc -l < $filename | tee -a $outfile`

Sponsored Links
    #5  
Old 02-09-2012
Registered User
 

Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by gary_w View Post
Try this:


Code:
vCount=`wc -l < $filename | tee -a $outfile`

Thanks, but I wanted the names of the files included in the details. Your suggestion added only the record count, but not the name of the file. Here's the final segment of code that worked:


Code:
do
  vCount=`wc -l $filename | tee -a $outfile | cut -d" " -f1`
  ((verifyRecordCount=verifyRecordCount+${vCount}))
done
echo "Total Record Count in Files: "$verifyRecordCount>>$outfile

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix "look" Command "File too large" Error Message shishong UNIX for Dummies Questions & Answers 14 05-30-2011 03:47 PM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 07:59 AM
Command Character size limit in the "sh" and "bourne" shell Roshan1286 UNIX for Advanced & Expert Users 1 10-29-2009 06:01 AM
error "test: [-d: unary operator expected" very confused. orionrush Shell Programming and Scripting 4 06-14-2009 10:27 AM
Results from "ls" command scecilia UNIX for Advanced & Expert Users 8 11-24-2003 03:14 PM



All times are GMT -4. The time now is 03:45 AM.