pipe then hyphen (stdin) as an alternative to for loop

user171558 :

I wrote a few sed an awk commands to extract a set of IDs that are associated with file names. I would like to run a set of commands using these filenames from id.txt

cat id.txt

14235.gz
41231.gz
41234.gz

I usually write for loops as follows:

for i in $(cat id.txt);
do 
    command <options> $i
done

I thought I could also do cat id.txt | command <options> -

Is there a way to pipe the output of cat, awk, sed, etc, line by line into a command?

Barmar :

Commands don't usually get their filename arguments on standard input. Using - as an argument means to read the file contents from standard input instead of a named file, it doesn't mean to get the filename from stdin.

You can use command substitution to use the contents of the file as all the filename arguments to the command:

command <options> $(cat id.txt)

or you can use xargs

xargs command <options> < id.txt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12167&siteId=1