awk pipe to a function? - Unix

This is a discussion on awk pipe to a function? - Unix ; You know how you can pipe the output of awk to the shell? Something like awk '{print "cp " $1 " " $2}' filenames | bash Well, can you call a defined shell function in a similar way? The following ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: awk pipe to a function?

  1. awk pipe to a function?

    You know how you can pipe the output of awk to the shell? Something like

    awk '{print "cp " $1 " " $2}' filenames | bash

    Well, can you call a defined shell function in a similar way? The
    following example may be a little contrived but it represents what I am
    trying to do:

    #!/bin/bash
    function func {
    cp $1 $2
    }
    awk '{print "func " $1 " " $2}' filenames | please_execute

    When you've been using unix for a while, you get a feel for what ought
    to be possible. This is how it is with this question. I've looked in
    several books but just cannot find what should replace "please_execute".
    Piping the output to a shell script file works but apparently you can't
    pipe to a user-defined function.

    I would be really grateful for any help.

    Phil Brown


  2. Re: awk pipe to a function?

    Begin
    On 2005-12-08, Phil Brown wrote:
    > I've looked in several books but just cannot find what should replace
    > "please_execute".


    It is called ``eval''.


    --
    j p d (at) d s b (dot) t u d e l f t (dot) n l .
    This message was originally posted on Usenet in plain text.
    Any other representation, additions, or changes do not have my
    consent and may be a violation of international copyright law.

  3. Re: awk pipe to a function?

    On 2005-12-08, Phil Brown wrote:
    > You know how you can pipe the output of awk to the shell? Something like
    >
    > awk '{print "cp " $1 " " $2}' filenames | bash
    >
    > Well, can you call a defined shell function in a similar way? The
    > following example may be a little contrived but it represents what I am
    > trying to do:
    >
    > #!/bin/bash
    > function func {
    > cp $1 $2
    > }
    > awk '{print "func " $1 " " $2}' filenames | please_execute
    >
    > When you've been using unix for a while, you get a feel for what ought
    > to be possible. This is how it is with this question. I've looked in
    > several books but just cannot find what should replace "please_execute".
    > Piping the output to a shell script file works but apparently you can't
    > pipe to a user-defined function.


    Put the function in a file and use BASH_ENV:

    export BASH_ENV=/path/to/file/containing/function
    awk '{print "func " $1 " " $2}' filenames | bash

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

+ Reply to Thread