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
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
Email: arn...@skeeve.com | of perl?
| -- Arnold Robbins
>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
--
Brian_...@CSi.com (Brian dot Inglis at SystematicSw dot ab dot ca)
use address above to reply