• 1 reply
  • Latest Post - ‏2011-01-31T12:47:53Z by ishields
2 Posts

Pinned topic one stdout into multiple stdout

‏2011-01-20T09:34:10Z |
is it possible to redirect std-out into multiple st-dins?


echo 'transfered data summary:'
nice -n19 bzcat *.bz2 |\
nice -n19 grep /przykl |\
nice -n19 uniq |\
nice -n19 awk 'BEGIN {FS="\""} {print $3}' |\
nice -n19 awk 'BEGIN {FS=" "} {print $2}' |\
nice -n19 awk '{ suma+=$0 } { print suma }' |\
nice -n19 tail -n 1

echo 'File request count:'
nice -n19 bzcat *.bz2 |\
nice -n19 grep /przykl |\
nice -n19 uniq |\
nice -n19 wc -l

echo 'End'

Files are huge logs (gigabystes) and i don't want to save any data during this proces into files just display results.
and I want to make this operation just once:
nice -n19 bzcat *.bz2 |\
nice -n19 grep /przykl |\
nice -n19 uniq |\
and redirect it's out into two inputs (wc and awk)

Is it possible and how?
Can someone give me an example?
Updated on 2011-01-31T12:47:53Z at 2011-01-31T12:47:53Z by ishields
  • ishields
    988 Posts

    Re: one stdout into multiple stdout

    I think the question here is really "Is it possible to run two commands using a single stdin?"

    The answer is "yes". You do this by duplicating stdin (file descriptor 0) to another file descriptor (say fd 7). The following bash script is a simple example showing how to print the first word of each input line and the total number of lines using this technique.

    #!/bin/bash awk 
    ' { print $1 } ' 7<&0 wc -l

    The info pages for bash (info bash) have more information on redirecting and duplicating file descriptors.

    Ian Shields