Direct execution of standard output
Direct execution of standard output
am 18.01.2008 10:01:29 von Kenneth Brun Nielsen
How can I directly execute the output of a awk command?
E.g.
find . -name *.pdf | awk '{print "cp --parent " $1 " /new/
location/."}'
Normally I would write it to a file, and execute that file afterwards.
But in simple cases it would be nice to do it directly.
Re: Direct execution of standard output
am 18.01.2008 10:45:50 von Joachim Schmitz
Kenneth Brun Nielsen wrote:
> How can I directly execute the output of a awk command?
>
> E.g.
> find . -name *.pdf | awk '{print "cp --parent " $1 " /new/
> location/."}'
find . -name *.pdf | xargs -l cp --parent '{}' /new/
> Normally I would write it to a file, and execute that file afterwards.
> But in simple cases it would be nice to do it directly.
Bye, Jojo
Re: Direct execution of standard output
am 18.01.2008 11:11:55 von Stephane CHAZELAS
On Fri, 18 Jan 2008 01:01:29 -0800 (PST), Kenneth Brun Nielsen wrote:
> How can I directly execute the output of a awk command?
>
> E.g.
> find . -name *.pdf | awk '{print "cp --parent " $1 " /new/
> location/."}'
>
> Normally I would write it to a file, and execute that file afterwards.
> But in simple cases it would be nice to do it directly.
Simply pipe it to sh. But note that it is a very dangerous thing
to do as you don't take into account filenames that may contain
shell special characters (spaces, $, quotes, ;, |...).
You also forgot to quote *.pdf.
Use the -exec action of find. Or far better, use pax or cpio
instead of cp.
--
Stephane
Re: Direct execution of standard output
am 18.01.2008 12:02:22 von Kenneth Brun Nielsen
On 18 Jan., 10:45, "Joachim Schmitz"
wrote:
> Kenneth Brun Nielsen wrote:
> > How can I directly execute the output of a awk command?
>
> > E.g.
> > find . -name *.pdf | awk '{print "cp --parent " $1 " /new/
> > location/."}'
>
> find . -name *.pdf | xargs -l cp --parent '{}' /new/
Great - thanks! Reading the man pages, I don't understand why you
suggest the "-l" argument (something with max-lines). Can you please
clarify this?
/Kenneth
Re: Direct execution of standard output
am 18.01.2008 12:03:06 von Kenneth Brun Nielsen
On 18 Jan., 11:11, Stephane Chazelas
wrote:
> On Fri, 18 Jan 2008 01:01:29 -0800 (PST), Kenneth Brun Nielsen wrote:
> > How can I directly execute the output of a awk command?
>
> > E.g.
> > find . -name *.pdf | awk '{print "cp --parent " $1 " /new/
> > location/."}'
>
> > Normally I would write it to a file, and execute that file afterwards.
> > But in simple cases it would be nice to do it directly.
>
> Simply pipe it to sh. But note that it is a very dangerous thing
> to do as you don't take into account filenames that may contain
> shell special characters (spaces, $, quotes, ;, |...).
>
> You also forgot to quote *.pdf.
>
> Use the -exec action of find. Or far better, use pax or cpio
> instead of cp.
Thanks, Stephane.
/Kenneth
Re: Direct execution of standard output
am 18.01.2008 12:44:15 von Joachim Schmitz
Kenneth Brun Nielsen wrote:
> On 18 Jan., 10:45, "Joachim Schmitz"
> wrote:
>> Kenneth Brun Nielsen wrote:
>>> How can I directly execute the output of a awk command?
>>
>>> E.g.
>>> find . -name *.pdf | awk '{print "cp --parent " $1 " /new/
>>> location/."}'
>>
>> find . -name *.pdf | xargs -l cp --parent '{}' /new/
>
> Great - thanks! Reading the man pages, I don't understand why you
> suggest the "-l" argument (something with max-lines). Can you please
> clarify this?
I guess you can drop the -l, but is was the closest match to your awk thing
which generated the same number of cp invocations (one per file found by
find) and I wasn't sure about the (non-standard) --parent option of ls
Bye, Jojo