Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

sh

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

into a single pipeline? If I don't need NUL separators 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 a follow-up to 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:

https://stackoverflow.com/a/143172/8207082

Edited to use my_command instead of command.

Follow-up question:

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

like image 912
cjfp Avatar asked Oct 23 '25 18:10

cjfp


2 Answers

You can just change the tee to point to proc sub, then do the exact same thing in there.

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

The only issue with using tee this way, is that if the xargs command also prints to screen, then it is possible for all the output to get jumbled since both the pipe and process sub are asynchronous.

like image 153
123 Avatar answered Oct 26 '25 09:10

123


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 with exec on fly which is connected to tr's stdin via standard process substitution. tee passes everything to stdout (ending on xargs), and a duplicate to a tr subprocess that outputs to /dev/tty.

like image 28
randomir Avatar answered Oct 26 '25 09:10

randomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!