0

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.

  • 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
  • 4
    What's wrong with -exec on the find? – Mel Boyce Jun 23 '13 at 9:19
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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