How do I display intermediate pipeline results for NUL-separated data?

How can I combine the following two commands:

find . -print0 | grep -z pattern | tr '\0' '\n'
find . -print0 | grep -z pattern | xargs -0 my_command

      

in one conveyor? If I don't need NUL delimiters, then I can do:

find . | grep pattern | tee /dev/tty | xargs my_command

      

I want to avoid using a temporary file like this:

find . -print0 | grep -z pattern > tempfile
cat tempfile | tr '\0' '\n'
cat tempfile | xargs -0 my_command
rm tempfile

      

This question is an extension of these answers:

1) Using / dev / tty to display intermediate pipeline results:

https://unix.stackexchange.com/a/178754/8207082

2) Using a NUL-separated list of files:

stack overflow

Edited to use my_command

instead command

.

Follow-up question:

Makefile rule that writes to / dev / tty inside a subshell?

+3


source to share


3 answers


You can just change tee to point to sub and then do the same.

   find . -print0 | grep -z pattern | tee >(tr '\0' '\n' > /dev/tty) | xargs -0 command

      



The only problem with using tee this way is that if the xargs command also prints to the screen, then it is possible that all the output is mixed, since both pipes and the process sub-process are asynchronous.

+5


source


One of the possibilities:

find . -print0 | grep -z pattern | { exec {fd}> >(tr '\0' '\n' >/dev/tty); tee "/dev/fd/$fd"; } | xargs -0 command

      



Where we create a temporary file descriptor fd

from exec

on the fly which is connected to tr

stdin via standard process replacement. tee

passes everything to stdout (ends with xargs

) and a duplicate to the subprocess tr

that is output to /dev/tty

.

+1


source


It is possible to execute multiple commands using xargs like this:

find . -print0 | grep -z pattern | xargs -0 -I% sh -c 'echo "%"; command "%"'

      

Source:

fooobar.com/questions/34160 / ...

In the course of discussion, the above is not safe, this is much better:

find . -print0 | grep -z pattern | xargs -0 -n 1 sh -c 'echo "$1"; my_command "$1"' _

      

-1


source







All Articles