Bash: using multiple values from an input pipeline to construct and execute a command

If you have multiple values coming through the Bash input pipeline, it can be difficult to process these into a complex, formatted set of arguments unless you use intermediate temporary files.

But one way to neatly put together a complex set of arguments from an input pipeline with multiple values is to use awk printf to format the exact command you desire, then have xargs execute it in its entirety (instead of relying on xargs limited formatting control using {}).   Here is an example:

# view csv output coming from file listing
find . -ls | tr -s ' ' ,

# apply complex formatting and reordering
# then execute the command using xargs
find . -ls | tr -s ' ' , | awk -F',' '{ printf "echo the file %s is --size=%d Bytes and owned by %s\n",$12,$8,$6 }' | xargs -L 1 -I {} bash -c "{}"

In this simple example, we just echo an English sentence that includes the filename, size, and owner in a reordered fashion.

the file ./experiment_startswith_list.py is --size=2227 Bytes and owned by fabian
the file ./inspect_func_test_decorator.py is --size=3420 Bytes and owned by fabian

However, it is not difficult to imagine constructing a real command that required arguments, multiple flags, etc.

REFERENCES

unix.stackexchange.com, xargs input in eval command