Creating your own Header file in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating your own Header file in awk
# 1  
Old 09-17-2013
Creating your own Header file in awk

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


Code:
$ cat foo.h
int add(int a,int b)
{
return(a+b);
}

Code:
$ 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..
# 2  
Old 09-17-2013
awk doesn't have an 'include' statement, but you can have more than one -f parameter to run multiple files, concatenating their code together.

Code:
$ 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

$

# 3  
Old 09-17-2013
Thank You so much...
# 4  
Old 09-17-2013
If your using gnu awk you have @include:

Code:
@include "foo.awk"
{ print add($1, $2) }

These 3 Users Gave Thanks to Chubler_XL For This Post:
# 5  
Old 09-18-2013
Hi Chubler_XL

my awk is this
Code:
$ awk --version
GNU Awk 3.1.8
Copyright (C) 1989, 1991-2010 Free Software Foundation.

how to run in method you suggested?
Code:
$ cat foo.awk
function add(a,b) {
        return(a+b)
}

Code:
$ cat main.awk
@include "foo.awk"
{ print add($1, $2) }

Code:
$ echo 1 2 | awk -f main.awk
awk: main.awk:1: @include "foo.awk"
awk: main.awk:1: ^ invalid char '@' in expression

# 6  
Old 09-18-2013
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!

Code:
$ 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


Last edited by Chubler_XL; 09-18-2013 at 01:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. UNIX for Dummies Questions & Answers

Creating a csv file with header in UNIX

I have a flat file that contains dynamic list of variables like a=1 b=2 c=3 . .. z=26 I need to convert the above into a csv file having the format below: a,b,c,..,z 1,2,3,..,26 Please note, I do not want a comma separating the last variable. I tried to refer the post... (4 Replies)
Discussion started by: vkumbhakarna
4 Replies

3. Shell Programming and Scripting

Average, min and max in file with header, using awk

Hi, I have a file which looks like this: FID IID MISS_PHENO N_MISS N_GENO F_MISS 12AB43131 12AB43131 N 17774 906341 0.01961 65HJ87451 65HJ87451 N 10149 906341 0.0112 43JJ21345 43JJ21345 N 2826 906341 0.003118I would... (11 Replies)
Discussion started by: kayakj
11 Replies

4. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

5. Shell Programming and Scripting

Adding header to sub files after splitting the main file using AWK

Hi Folks, I have a file like: mainfile.txt: ------------- file1 abc def xyz file1 aaa pqr xyz file2 lmn ghi xyz file2 bbb tuv xyz I need output having two files file1 and file2. file1: ------ Name State Country abc def xyz aaa pqr xyz file2: (3 Replies)
Discussion started by: tanmay.gemini
3 Replies

6. Shell Programming and Scripting

awk print header as text from separate file with getline

I would like to print the output beginning with a header from a seperate file like this: awk 'BEGIN{FS="_";print ((getline < "header.txt")>0)} { if (! ($0 ~ /EL/ ) print }" input.txtWhat am i doing wrong? (4 Replies)
Discussion started by: sdf
4 Replies

7. Shell Programming and Scripting

Using AWK BEGIN to extract file header info into variables

Hi Folks, I've searched for this for quite a while, but can't find any solution - hope someone can help. I have various files with standard headers. eg. <HEADER> IP: 1.2.3.4 Username: Joe Time: 12:00:00 Date: 23/05/2010 </HEADER> This is a test and this part can be any size... (6 Replies)
Discussion started by: damoske
6 Replies

8. Shell Programming and Scripting

Creating Header & Trailer for bulk volume data file

Hi all, I have a requirement to create a Header &Trailer for a file which is having 20 millions of records. If I use the following method, i think it will take more time. cat "Header"> file1.txt cat Data_File>>file1.txt cat "Trailer">>file1.txt since second CAT command has to read all... (4 Replies)
Discussion started by: Raamc
4 Replies

9. Shell Programming and Scripting

Manipulating a header file using awk or sed

Hi Guys, Is there a simple way of doing the below. Available<spaces>Assigned<spaces>Maximum<spaces>Maximum<spaces>Page<spaces>Total <spaces>Used<spaces>Pct<spaces>Max. Pct<CR> Space<spaces>Capacity<spaces>Extension<spaces>Reduction<spaces>Size<spaces>... (8 Replies)
Discussion started by: eo29
8 Replies

10. UNIX for Dummies Questions & Answers

Creating a header.

Hi all, Suppose i wish to have a file which should be something like this: ROOH20020406080015000003 RLO00220404144142ENG01 RLO00220404123602ENG01 RLO00220404123602ENG02 As u can seeit contains 3 files. Basically I want the header in the file to be something like this. here ROOH will... (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question