Confused over results using "tee" in piped command


 
Thread Tools Search this Thread
# 1  
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
# 2  
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
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  
Try this:

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

# 5  
Quote:
Originally Posted by gary_w
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

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #97
Difficulty: Easy
Unix is a multi-user operating system where the same resources can be shared by different users even if they do not have the proper permissions to do so.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mindboggling difference between using "tee" and "/usr/bin/tee" in bash

I'm on Ubuntu 14.04 and I manually updated my coreutils so that "tee" is now on version 8.27 I was running a script using bash where there is some write to pipe error at some point causing the tee command to exit abruptly while the script continues to run. The newer version of tee seems to prevent... (2 Replies)
Discussion started by: stompadon
2 Replies

2. UNIX for Beginners Questions & Answers

Append content using "tee" command

Hi, How to append content into a file using tee command echo " file1 is archived"| tee -a archive.txt echo " file2 is archived"| tee -a archive.txt echo " file3 is archived"| tee -a archive.txt how to append content as new rows in the archive.txt Thanks, Srinadh. (4 Replies)
Discussion started by: srinadhreddy27
4 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Short command to create two files >{respo,nd}.php (with "tee" command?)

08:29 < xsi> >{respo,nd}.php bash: {respo,nd}.php: ambiguous redirect 08:31 < geirha> xsi: maybe you want tee So I was advised to do so. And I can't create two OR MORE files at once with {a,b,c,d,e,f}.php (which I quickly now need to create and to learn to create in the future to quickly... (2 Replies)
Discussion started by: Xcislav
2 Replies

5. Shell Programming and Scripting

The pipe not use "tee" to print on the screen for specific function

I have code fragment like { aa bb cc } > $LOG aa bb cc, all call function "ff", I want "ff" to print on the screen,but others do not print on the scree, is there a method? I can't use "tee", becasue tee I meet the write "error" ff() { echo "hello" } (2 Replies)
Discussion started by: yanglei_fage
2 Replies

6. Homework & Coursework Questions

C++ with Linux - writing a "tee"-like function

Greetings, everyone. 1. The problem statement, all variables and given/known data: I'm running into a problem with my program concerning the actual output it does. When I open the file that gets the output, it contains a large number of hex(?) variables and not what the user wants. The... (0 Replies)
Discussion started by: assignmentoper
0 Replies

7. Shell Programming and Scripting

Screen output is blocked by "| tee" command

BACK STORY: I have a script build.py . (It's for creating the ISO file for a special edition of Swift Linux.) This build.py script executes the mintConstructor.py script that I use to modify the Regular Swift Linux ISO to get the special edition Swift Linux ISO. The lines of the script that... (2 Replies)
Discussion started by: swiftlinux
2 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

error "test: [-d: unary operator expected" very confused.

Im trying to check if a series of directory exists and if not create them, and am having issues. All the instances of test return with the error "test: #!/bin/bash location_Parent=~/Documents/sight_of_sound location_IMG=~/Documents/Sight_of_sound/IMG location_AUD=~/Documents/Sight_of_sound/AUD... (4 Replies)
Discussion started by: orionrush
4 Replies

10. UNIX for Advanced & Expert Users

Results from "ls" command

Hello, I am uning IRIX64. I need to do an ls - 1 (one) command in order to get a one colum listing of the contents of a non current directory: t01:~ > ls -1 /scratch/u118807/reconstruct ./ ../ fr000202.rec fr000202.sin input_files/ input_files.tar sin000000.rec sin000000.sin... (8 Replies)
Discussion started by: scecilia
8 Replies

Featured Tech Videos