Awk dealing with pipe. 
Author Message
 Awk dealing with pipe.

Hello all,

      I just started with awk and sed, I am more of a perl/C/C++ person.  I
have a quick question reguarding the pipe.  In Awk, I am trying to use this
construct.

                      while ((getline < "somedata.txt") > 0)
                               {print | "mv"} #or could be "mv -v" for
verbose. Is it possible that "print" is no longer printing the value of
getline, if so how do I correct it?

I have the following two books, one by O'Reilly, SED and AWK and the one by
Arnold Robbins, Effective perl programming, both of these books have the
construct and the explanation that says it should work it will not.

Here is what Robbins said:
print items | command
It is also possible to send output to another program through a pipe instead
of into a file. This type of redirection opens a pipe to command and writes
the values of items through this pipe, to another process created to execute
command. The redirection argument command is actually an awk expression. Its
value is converted to a string, whose contents give the shell command to be
run. For example, this produces two files, one unsorted list of BBS names
and one list sorted in reverse alphabetical order:
awk '{ print $1 > "names.unsorted"
       command = "sort -r > names.sorted"
       print $1 | command }' BBS-list
basically the other book said the same with a small change in construct.  I
am running redhat 6.2 and the bash shell.  The shell tells me I am missing
the arguments required after the mv command.

I have ways around this but I would really like to know why this won't work.

Any help would be great

Andrew Eaton
Ushost Service Providers



Sat, 09 Nov 2002 03:00:00 GMT  
 Awk dealing with pipe.

Quote:

>Hello all,

>      I just started with awk and sed, I am more of a perl/C/C++ person.  I
>have a quick question reguarding the pipe.  In Awk, I am trying to use this
>construct.

>                      while ((getline < "somedata.txt") > 0)
>                               {print | "mv"} #or could be "mv -v" for
>verbose. Is it possible that "print" is no longer printing the value of
>getline, if so how do I correct it?

The problem here is that `mv' doesn't read standard input, it only
processes command lines.  Assuming that your data is something like:

        oldfile newfile

You can do things two ways:

        # build the command and execute it
        while ((getline < "somedata.txt") > 0) {
                command = "mv " $1 " " $2
                system(command)
        }
        close("somedata.txt")

or this way:

        # send commands to the shell
        while ((getline < "somedata.txt") > 0) {
                printf("mv %s %s\n", $1, $2) | "sh"
        }
        close("somedata.txt")
        close("sh")

The latter is more efficient.

I hope this helps,

Arnold
--
Arnold Robbins                  | If There's More Than One Way To Do It,
Guest account at Emory Math/CS  | then why is there only one implementation

                                |       -- Arnold Robbins



Sun, 10 Nov 2002 03:00:00 GMT  
 Awk dealing with pipe.
On Tue, 23 May 2000 12:21:35 -0500, "Andrew Eaton"

Quote:

>Hello all,

>      I just started with awk and sed, I am more of a perl/C/C++ person.  I
>have a quick question reguarding the pipe.  In Awk, I am trying to use this
>construct.

>                      while ((getline < "somedata.txt") > 0)
>                               {print | "mv"} #or could be "mv -v" for
>verbose. Is it possible that "print" is no longer printing the value of
>getline, if so how do I correct it?

>I have the following two books, one by O'Reilly, SED and AWK and the one by
>Arnold Robbins, Effective perl programming, both of these books have the
>construct and the explanation that says it should work it will not.

>Here is what Robbins said:
>print items | command
>It is also possible to send output to another program through a pipe instead
>of into a file. This type of redirection opens a pipe to command and writes
>the values of items through this pipe, to another process created to execute
>command. The redirection argument command is actually an awk expression. Its
>value is converted to a string, whose contents give the shell command to be
>run. For example, this produces two files, one unsorted list of BBS names
>and one list sorted in reverse alphabetical order:
>awk '{ print $1 > "names.unsorted"
>       command = "sort -r > names.sorted"
>       print $1 | command }' BBS-list
>basically the other book said the same with a small change in construct.  I
>am running redhat 6.2 and the bash shell.  The shell tells me I am missing
>the arguments required after the mv command.

>I have ways around this but I would really like to know why this won't work.

>Any help would be great

>Andrew Eaton
>Ushost Service Providers

mv and other similar commands do not read standard input, they
"read" command line arguments, so you have to use the system()
function:

        system( "mv " $0 )

assuming $0 follows the rules for mv arguments, including $IFS
argument separators.

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

                                use address above to reply



Thu, 14 Nov 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. dealing with a dead pipe

2. Dealing with Magnus Kempe (was Dealing with hecklers)

3. piping input files into awk script

4. awk pipe as bash variable?

5. Piping out of & into awk

6. piping to awk in tcl

7. Two pipes within awk....

8. Awk and pipes

9. HELP: piping info into awk.

10. piping to awk in tcl

11. pipe.close -> Broken pipe/DRb madness

12. Turning off pipe buffering, or flushing pipe?

 

 
Powered by phpBB® Forum Software