Let's assume that command1 is processing something and I am interested in BOTH output of command1 as well as how many lines the output actually had.
$ command1 | wc - l
prints number of lines of output of command1, while
$ command1 | nl
prints something like that:
1 ./PaDe014
2 ./PaDe033
3 ./PaDe001
4 ./PaDe013
5 ./PaDe025
6 ./PaDe028
See How to count lines in a document? for more solutions.
However I am interested to generate output like that:
./PaDe014
./PaDe033
./PaDe001
./PaDe013
./PaDe025
./PaDe028
Total number of files generated: 6
I have a vague feeling that it can be achieved with tee
and wc
, but cannot figure out how exactly.
What is the simplest way to achieve desired output?
I also tried:
command1 | tee >(wc -l)
but there must be a race condition here, as from time to time I receive strange results. Here is the test output:
pdebski@PaDe:~$ (ls -l ; printf "Total: ") | tee -a >(wc -l)
total 56
drwxr-xr-x 7 pdebski pdebski 4096 cze 22 18:50 Data
drwxr-xr-x 2 pdebski pdebski 4096 cze 22 15:14 Desktop
drwxr-xr-x 2 pdebski pdebski 4096 cze 22 14:50 Documents
drwxr-xr-x 2 pdebski pdebski 4096 cze 16 01:09 Downloads
-rw-r--r-- 1 pdebski pdebski 8980 cze 16 01:04 examples.desktop
drwxr-xr-x 4 pdebski pdebski 4096 cze 17 13:44 Music
drwxr-xr-x 2 pdebski pdebski 4096 cze 22 13:14 Pictures
drwxr-xr-x 2 pdebski pdebski 4096 cze 16 01:09 Public
drwxr-xr-x 2 pdebski pdebski 4096 cze 16 01:09 Templates
drwxrwxr-x 2 pdebski pdebski 4096 cze 22 01:21 test
drwxrwxr-x 2 pdebski pdebski 4096 cze 22 01:21 test2
drwxr-xr-x 2 pdebski pdebski 4096 cze 16 01:09 Videos
Total: pdebski@PaDe:~$ 13
A nice illustration for the need of Dijkstra's Semaphores and Mutexes, is not it? (see https://en.wikipedia.org/wiki/Edsger_W._Dijkstra )
Apparently there should be some wait
here.
Same results with:
$ (ls -l && printf "Total: ") | tee -a >(wc -l)