HOW2 call a function from inside awk ?

HOW2 call a function from inside awk ?

Post by no-toppos » Sun, 13 Sep 2009 07:04:46


I've tried to get an answer to this problem before.
Now I'll try a simplified explanation:-

The following 'script' :-
DV | gawk ' BEGIN { FS =")"}
/kdeinit/{D++; V=1; print D "," V $4 };
!/kdeinit/{ V++; print D "," V $3 };
END {print D "," V}' | gawk '/mc/'

gives me:-
1,1---mc(13529
1,3---mc(13570
1,4---mc(9837
2,1---mc(1593
2,2---mc(1824
2,4---mc(304
3,1---mc(17614
3,2---mc(2429
3,3---mc(13406
3,5---mc(8952
4,2---mc(1477
4,3---mc(1549
4,4---mc(13925
5,1---mc(29546
5,2---mc(29295
5,3---mc(3538
5,5---mc(17791

All I'm interested in is the variable "D", which is the left-most char of
the output.

When I modify the handling of "D" in the script slightly to:-
# DV | gawk ' BEGIN { FS =")"}
/kdeinit/{V=1; print $1 "," V $4 };
!/kdeinit/{ V++; print D "," V $3 };
END {print D "," V}' | gawk '/mc/'
I get:-
|-kdeinit(1186,1---mc(13529
,3---mc(13570
,4---mc(9837
|-kdeinit(1188,1---mc(1593
,2---mc(1824
,4---mc(304
|-kdeinit(1189,1---mc(17614
,2---mc(2429
,3---mc(13406
,5---mc(8952
,2---mc(1477
,3---mc(1549
,4---mc(13925
|-kdeinit(23986,1---mc(29546
,2---mc(29295
,3---mc(3538
,5---mc(17791

What I finally need is the "|-kdeinit(<pidNumber>" to be replaced
by the index 1 or 2,3,4 [for which of 4 desktops].
Which could be done eg. by returning the line in a file which
contains the 4 pids, in 4 lines.

So a first step could be to transform:
eg. "|-kdeinit(23986" to "23986"
which: eg. sed s/^"|-kdeinit("//
does.

My problem seems to be, not knowing if/how to nest piped transformations
INSIDE awk ?

Here's my final error log:-
# DV | gawk ' BEGIN { FS =")"}
gawk: cmd. line:2: /kdeinit/{kint=$1; pid=`ed s/^"|-kdeinit("// kint`;
gawk: cmd. line:2: ^ invalid char '`' in expression
/usr/local/bin/DV: line 2: 15737 Done pstree -p
15738 | cut -b25-
15739 Broken pipe | grep su

OK, I've just confirmed that awk can't even handle:
/kdeinit/{kint=$1; pid=`pwd`; ...

So how do I call functions [subroutines that accept an arg and return a value]
from inside awk ?

Thanks for any feedback.

== Chris Glur.
 
 
 

HOW2 call a function from inside awk ?

Post by Janis Papa » Sun, 13 Sep 2009 08:30:44


And you've got and ignored the answers.


[snip 80+ lines of trash]


Short answer: retry.

Longer answer (copied from a reply to your first thread):
"Please post some small sample input and expected out as it's very
unclear from the above what the input to the awk script looks like
and what you expect to get out the other end."

 
 
 

HOW2 call a function from inside awk ?

Post by Ed Morto » Sun, 13 Sep 2009 11:29:45


<snip>

The awk equivalent to that sed command would be:

sub(/^[|]-kdeinit[(]/,"")

The additional []s are because awk uses ERE whereas sed uses BREs.
> > My problem seems to be, not knowing if/how to nest piped transformations> > INSIDE awk ?

The solutions is to not do that, it's rarely necessary.
> > Here's my final error log:-> > # DV | gawk ' BEGIN { FS =")>}> kdeinit/{kint=$1; pid=`ed s/^"|-kdeinit("// kint`;

How would you call a shell command from within a C or other program?
Would you expect to just enclose the command in ticks and it'd work?
Of course not, and neither will it in an awk program.> > > > rint pid; V=1; print $1 ",">V>$4 };
> > /kdeinit/{ V++; print D ">">V $3 };
> > ND {print D "," V}' | >aw> '/mc/'
>
> gawk: cmd. line:2: kdeinit/{kint=$1; pid=`ed s/^"|-kdein>t("// kint`;
> gawk: cmd. line:2: ^ inva>id char '`' in expression
> /usr/local/bin/DV: line 2: 1573> Done stree ->
> 5738 |>cu> -b25-
> 5739 Broken pipe | grep su> >
> OK, I've just confirmed that awk can't even handle:
> kdeinit/{kint=$1; pid=`pwd`; ...

It's not that it "can't even handle" it, it's not supposed to handle
it. That is just not the syntax to call an external program from an
awk script. See the "system()" function if you >ea>ly want to do that,
but it's probably not necessary.

>
> So how do I call fu>ctions [subroutines that accept an arg and return a value]
> from inside awk ?

Like in any other programming language. Just define them and then call
them.

Ed.
 
 
 

HOW2 call a function from inside awk ?

Post by Ed Morto » Sun, 13 Sep 2009 23:08:03


And you've been given an answer before, you just haven't read it.

<snip>

> gawk: cmd. line:2: kdeinit/{kint=$1; pid=`ed s/^"|-kdeinit("// kint`;> > gawk: cmd. line:2: ^ invalid char '`' >n expression
> /usr/local/bin/DV: line 2: 15737 Done > stree -p
> 57>8 | cut -b25-
>>> 5739 Broken pipe | grep su
>
> OK, I'>e just confirmed that awk can't even handle:
> kdeinit/{kint=$1; pid=`pwd`; ...

All of your problems in this thread seem to be related to the fact you
didn't read the responses in the previous thread. In particular you
really need to understand that awk is NOT shell so you shouldn't
assume a shell syntax or that you can call shell scripts directly from
an awk program like you would from another shell script.

Given you want to do the equivalent of this in awk:

$ echo "a b c" | sed "s/b//"
a c

Here's the 2 possible syntaxes to call sed or other external tool from
awk:

$ echo "a b c" | awk '{system("echo " $0 " | sed \"s/b//\"")}'
a c
$ echo "a b c" | awk '{print | "sed \"s/b//\""}'
a c

neither of which is what you attempted with invalid shell-like syntax

$ echo "a b c" | awk '`sed "s/b//"`'
awk: `sed "s/b//"`
awk: ^ invalid char '`' in expression

but in reality you wouldn't do any of the above, you'd use the awk sub
() function:

$ echo "a b c" | awk '{sub(/b/,""); print}'
a c

Regards,

Ed.