can AWK read function from input file? 
Author Message
 can AWK read function from input file?

can I read the function directly from the data input file?

the input (file1) is like this:

120     250    $2-$1
  14       28    $2*2
  80       20    $1-$2+10

the result output (file2) must be like this:

120     250    130
  14       28      56
  80       20      70

assuming that the separator is [tab]   how will be the AWK command line to
do that?

Giovanni



Thu, 16 Jan 2003 03:00:00 GMT  
 can AWK read function from input file?


Quote:
>can I read the function directly from the data input file?

>the input (file1) is like this:

>120     250    $2-$1
>  14       28    $2*2
>  80       20    $1-$2+10

>the result output (file2) must be like this:

>120     250    130
>  14       28      56
>  80       20      70

>assuming that the separator is [tab]   how will be the AWK command line to
>do that?

>Giovanni

here's the awk program :-

bash-2.03$ cat eval3.awk
{
  cmd="awk 'BEGIN{print " $3 "}'"
  gsub(/\$1/,$1,cmd)
  gsub(/\$2/,$2,cmd)
  cmd | getline result
  close(cmd)
  print $1,$2,result

Quote:
}

here's the data file :-

bash-2.03$ cat eval3.dat
120     250    $2-$1
  14       28    $2*2
  80       20    $1-$2+10

here's the test run :-

bash-2.03$ gawk -f eval3.awk eval3.dat
120 250 130
14 28 56
80 20 70

bash-2.03$

--
Alan Linton



Thu, 16 Jan 2003 03:00:00 GMT  
 can AWK read function from input file?

Quote:

>can I read the function directly from the data input file?

The short answer is: "Not really".  No version of AWK that I've ever seen
has an "eval" type function.  One truly nice thing about Perl is that it
does.

However, if you really want to do this (and enjoy the intellectual exercise)
there are a couple of ways to kludge it - or, you could look in Deja and
find the most recent thread about "expression evaluation in AWK".  Note that
Alan Linton and my own good self were the major contributors to that thread.

Quote:
>the input (file1) is like this:

>120     250    $2-$1
>  14       28    $2*2
>  80       20    $1-$2+10

>the result output (file2) must be like this:

>120     250    130
>  14       28      56
>  80       20      70

>assuming that the separator is [tab]   how will be the AWK command line to
>do that?

Here's my solution:

#!/bin/gawk -f
BEGIN   { print "BEGIN {";print "OFS=\"\\t\"" }
{
print "$0 = \""$0"\""
gsub(/\t/,",")
print "print",$0

Quote:
}

END     { print "}" }

And the usage is: ./thisfile datafile | gawk -f-

Note that this is pretty generic.  Double quotes in the input file will
{*filter*}it up, though...



Thu, 16 Jan 2003 03:00:00 GMT  
 can AWK read function from input file?


Quote:


>>can I read the function directly from the data input file?

>The short answer is: "Not really".  No version of AWK that I've ever seen
>has an "eval" type function.  One truly nice thing about Perl is that it
>does.

>However, if you really want to do this (and enjoy the intellectual exercise)
>there are a couple of ways to kludge it - or, you could look in Deja and
>find the most recent thread about "expression evaluation in AWK".  Note that
>Alan Linton and my own good self were the major contributors to that thread.

>>the input (file1) is like this:

>>120     250    $2-$1
>>  14       28    $2*2
>>  80       20    $1-$2+10

>>the result output (file2) must be like this:

>>120     250    130
>>  14       28      56
>>  80       20      70

>>assuming that the separator is [tab]   how will be the AWK command line to
>>do that?

>Here's my solution:

>#!/bin/gawk -f
>BEGIN  { print "BEGIN {";print "OFS=\"\\t\"" }
>{
>print "$0 = \""$0"\""
>gsub(/\t/,",")
>print "print",$0
>}
>END    { print "}" }

>And the usage is: ./thisfile datafile | gawk -f-

>Note that this is pretty generic.  Double quotes in the input file will
>screw it up, though...

Hi Giovanni and Kenny,

I like the basic idea of your program Kenny. It uses only two processes
where mine uses one per line of input plus one. Also yours is more
general but I find that it can crash if the input data uses leading tabs
or if it has spaces instead of tabs as field separators.

I find this works:-

#!/bin/gawk -f
BEGIN   { print "BEGIN {";print "OFS=\"\\t\"" }
{
print "$0 = \""$0"\""
sub(/^[\t ]+/,"")
gsub(/[\t ]+/,",")
print "print",$0

Quote:
}

END     { print "}" }

For Brian Kernighan's awk95 you have to use "-f -" instead of "-f-"
which is accepted by gawk. This may also be true for other versions of
awk.
--
Alan Linton



Fri, 17 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Reading from input file writing to output file

2. Replacing a string from an input file within awk script

3. piping input files into awk script

4. Multiple input files to awk

5. input from data file into awk under WindowsNT

6. How to specify input file within awk script?

7. help on awk and ASCII file input

8. Q: Multi file input for AWK

9. Multiple input files in awk?

10. How to read a polynominal function from input

11. I need a function for read user input.

12. reading other input file??

 

 
Powered by phpBB® Forum Software