|
Home > Archive > Unix Shell > August 2007 > GAWK - Using system("command")
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
GAWK - Using system("command")
|
|
|
| Hi,
1) I would like to execute in gawk a shell command looking like this:
---------------------------------------------------------------
#!/bin/gawk -f
begin{
system("echo 'test' | gawk '{print $0}'")
}
---------------------------------------------------------------
but I get this error message:
---------------------------------------------------------------
gawk: cmd. line:1: '{print
gawk: cmd. line:1: ^ invalid char ''' in expression
---------------------------------------------------------------
2) How define functions in BEGIN{}
For example :
---------------------------------------------------------------
#!/bin/gawk -f
begin{
func test(var) # definition of some function
{
if (match($0,var)!=0)
{
print NR":\t"$0
}
}
while ("cat testfile" | getline > 0) # execution of this function
using the output of "cat filetest"
{
test("foo")
}
}
---------------------------------------------------------------
Or how to define a function we need to use in BEGIN{} ?
| |
| Bill Marcum 2007-08-25, 1:22 pm |
| On Sat, 25 Aug 2007 02:15:51 -0700, sylar
<ggray8@gmail.com> wrote:
>
>
> Hi,
>
> 1) I would like to execute in gawk a shell command looking like this:
>
> ---------------------------------------------------------------
> #!/bin/gawk -f
> begin{
> system("echo 'test' | gawk '{print $0}'")
> }
Awk is case sensitive. begin != BEGIN
Why do you want to execute gawk within gawk?
> ---------------------------------------------------------------
>
> but I get this error message:
>
> ---------------------------------------------------------------
> gawk: cmd. line:1: '{print
> gawk: cmd. line:1: ^ invalid char ''' in expression
It works for me. Are you using gawk under Windows? Single and double
quotes don't work the same in the Windows cmd.exe shell as in a Unix
shell.
> ---------------------------------------------------------------
>
> 2) How define functions in BEGIN{}
> For example :
>
> ---------------------------------------------------------------
> #!/bin/gawk -f
> begin{
> func test(var) # definition of some function
> {
> if (match($0,var)!=0)
> {
> print NR":\t"$0
> }
> }
> while ("cat testfile" | getline > 0) # execution of this function
> using the output of "cat filetest"
> {
> test("foo")
> }
> }
> ---------------------------------------------------------------
>
> Or how to define a function we need to use in BEGIN{} ?
>
Functions are global, they are not defined inside action blocks.
From "info gawk":
8.2.1 Function Definition Syntax
--------------------------------
Definitions of functions can appear anywhere between the rules of an
`awk' program. Thus, the general form of an `awk' program is extended
to include sequences of rules _and_ user-defined function definitions.
There is no need to put the definition of a function before all uses of
the function. This is because `awk' reads the entire program before
starting to execute any of it.
--
I should have been a country-western singer. After all, I'm older than
most western countries.
-- George Burns
| |
|
| > Awk is case sensitive. begin != BEGIN
Ok
> Why do you want to execute gawk within gawk?
For example, getting in an gawk program the result of a shell command
involving gawk, wget, etc
command | getline var
> It works for me. Are you using gawk under Windows? Single and double
> quotes don't work the same in the Windows cmd.exe shell as in a Unix
> shell.
I am using a mingw build of zsh but system() calls cmd.exe and not the
shell you are currently using with gawk.exe
So i must put double quotes or doing something like system(echo
command | sh)
> From "info gawk":
> 8.2.1 Function Definition Syntax
> --------------------------------
>
> Definitions of functions can appear anywhere between the rules of an
> `awk' program. Thus, the general form of an `awk' program is extended
> to include sequences of rules _and_ user-defined function definitions.
> There is no need to put the definition of a function before all uses of
> the function. This is because `awk' reads the entire program before
> starting to execute any of it.
Ok
Thanks for the answer.
| |
| Ed Morton 2007-08-25, 1:22 pm |
| sylar wrote:
> Hi,
>
> 1) I would like to execute in gawk a shell command looking like this:
Already answered in comp.lang.awk. Please don't multi-post (see
http://smjg.port5.com/faqs/usenet/xpost.html).
Ed.
|
|
|
|
|