Command like wc but with tee behaviour

io-redirectionpipeteetext processingwc

I want to backup a database using psql and COPY command. here is my script:

psql "user=${USERNAME} host=${HOSTNAME} dbname=${DBNAME} password=${PASSWORD}" -c \
"COPY (SELECT * FROM tbl) ORDER BY id ASC) TO STDOUT WITH CSV HEADER;" | \
bzip2 -z -f --best -c > /home/${DBNAME}-${FILENAME}.csv.bz2

Also I want to know how many lines are copied from database but psql does not have such feature. so I need external command. I need something like this:

psql ... | wc -l | bzip2

Is there any solution? The only solution I have found is to use fifo:

mkfifo /home/backup.fifo
psql ... | tee /home/backup.fifo | bzip2

Now from another terminal window I can use wc:

wc -l /home/backup.fifo

Best Answer

Both bash and zsh have a >(pipeline) feature:

psql ... | tee >(wc -l) | bzip2

Note that the > here is not a normal redirection, but a necessary part of the syntax. You would need a second > if you wanted to combine it with an actual redirection (with a space in between so it wouldn't be read as >> redirect-for-append).