Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

linux - How to pass command output as multiple arguments to another command

I want to pass each output from a command as multiple argument to a second command, e.g.:

grep "pattern" input

returns:

file1
file2
file3

and I want to copy these outputs, e.g:

cp file1  file1.bac
cp file2  file2.bac
cp file3  file3.bac

How can I do that in one go? Something like:

grep "pattern" input | cp $1  $1.bac
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use xargs:

grep 'pattern' input | xargs -I% cp "%" "%.bac"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...