Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The example below shows what I want to do, namely take the result from my last pipe and put it into the middle of the next command (where I wrote $RESULT_FROM_FIND_COMMAND).

$ find . -name "*0.01*txt" | cp $RESULT_FROM_FIND_COMMAND $(awk '{{split($1,a,"/")} {print a[3]"_"a[4]"_"a[5]}}')

What variable or command must I use to achieve this?

I'm using bash if that matters.

Ps. note that awk should also take the result from find as an input.

share|improve this question
    
Could probably do this with a for i in $(find . -name "*0.01*txt") do ..., but I would like a version that follows the format above. –  The Unfun Cat Jun 23 '13 at 7:52
1  
What about find . -name "*0.01*txt" | awk '{split($1,a,"/"); print "echo cp "$1" "a[3]"_"a[4]"_"a[5]}' | sh? (If satisfied by the output, remove the “echo” to perform copying.) –  manatwork Jun 23 '13 at 9:18
3  
What's wrong with -exec on the find? –  Mel Boyce Jun 23 '13 at 9:19
add comment

1 Answer

up vote 2 down vote accepted

In this case, use the shell to iterate

find . -name "*0.01*txt" | while IFS= read -r filename; do
    newname=$(awk '{split($1,a,"/"), print a[3]"_"a[4]"_"a[5]}' <<< "$filename")"
    cp "$filename" "$newname"
done

In general, xargs is helpful:

find .... -print0 | xargs -o -I FILE someCommand arg1 FILE arg3 FILE
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.