whether it is possible in awk to create your own header file like in C
if so someone give me awk equivalent for below one
$ cat foo.h
int add(int a,int b)
{
return(a+b);
}
$ cat main.c
#include<stdio.h>
#include"foo.h"
void main()
{
int number1=10,number2=10,number3;
number3 = add(number1,number2);
printf("Addition of Two numbers : %d\n\n",number3);
}
Thanks in advance..
awk doesn't have an 'include' statement, but you can have more than one -f parameter to run multiple files, concatenating their code together.
$ cat foo.awk
function add(a,b) {
return(a+b)
}
$ cat main.awk
{ print add($1, $2) }
$ echo "1 2" | awk -f foo.awk -f main.awk
3
$
If your using gnu awk you have @include:
@include "foo.awk"
{ print add($1, $2) }
3 Likes
Hi Chubler_XL
my awk is this
$ awk --version
GNU Awk 3.1.8
Copyright (C) 1989, 1991-2010 Free Software Foundation.
how to run in method you suggested?
$ cat foo.awk
function add(a,b) {
return(a+b)
}
$ cat main.awk
@include "foo.awk"
{ print add($1, $2) }
$ echo 1 2 | awk -f main.awk
awk: main.awk:1: @include "foo.awk"
awk: main.awk:1: ^ invalid char '@' in expression
Sorry, it seems @include was only added in awk 4.x.
However igawk is available on some distributions, and this does a pre-process of scripts to expand @include statements.
But it's all a bit of a mess as GNU awk 4.x requires quotes around filename and igawk will not work with them!
$ cat main1.awk
@include foo.awk
{ print add($1, $2) }
$ echo "2 3" | igawk -f main1.awk
5
$ cat main.awk
@include "foo.awk"
{ print add($1, $2) }
$ echo "2 3" | awk -f main.awk
5