Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How "awk -f" works?

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-14-2010
cosmologist cosmologist is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 28 June 2013, 3:19 PM EDT
Posts: 173
Thanks: 39
Thanked 0 Times in 0 Posts
How "awk -f" works?

Hi all,

I have hard time understanding what
Code:
awk -f

does.

For example I have the command:

Code:
 awk -f max.awk data.dat > results

What I know that this will run the script of max.dat onto the data in data.dat.

This is the content of the file max.awk:


Code:
{ getline < "output.dat" }


       {maxmage = $2 + 1}



{print maxmage }


My question is: $2 is the second column of the file "output.dat", correct?
Then what is the role of the file data.data in the awk -f script?

I've read all the sources online but I can't find an answer LinuxLinux
Sponsored Links
    #2  
Old Unix and Linux 05-14-2010
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 29 May 2015, 2:09 AM EDT
Location: Zürich
Posts: 8,556
Thanks: 337
Thanked 1,078 Times in 936 Posts
Correct.

AWK will read the file data.dat - line by line. You unconditionally use getline to read a line of output.dat - which replaces the line ($0) from data.dat, which you then print.

When there's no more lines to read from data.dat, you're done. When there's no more lines of output.dat to read, the remaining lines of data.dat are read and unaffected by the getline.


Code:
$ cat data.dat
file 1 line 1
file 1 line 2
file 1 line 3

$ cat output.dat
file 2 line 1
file 2 line 2

$ cat awk.in
{ getline < "output.dat" }
1

$ awk -f awk.in data.dat
file 2 line 1
file 2 line 2
file 1 line 3

The Following User Says Thank You to Scott For This Useful Post:
cosmologist (05-15-2010)
Sponsored Links
    #3  
Old Unix and Linux 05-15-2010
cosmologist cosmologist is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 28 June 2013, 3:19 PM EDT
Posts: 173
Thanks: 39
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by scottn View Post
Correct.

AWK will read the file data.dat - line by line. You unconditionally use getline to read a line of output.dat - which replaces the line ($0) from data.dat, which you then print.

When there's no more lines to read from data.dat, you're done. When there's no more lines of output.dat to read, the remaining lines of data.dat are read and unaffected by the getline.


Code:
$ cat data.dat
file 1 line 1
file 1 line 2
file 1 line 3

$ cat output.dat
file 2 line 1
file 2 line 2

$ cat awk.in
{ getline < "output.dat" }
1

$ awk -f awk.in data.dat
file 2 line 1
file 2 line 2
file 1 line 3



So then if the number of lines in output.dat and in data.dat is the same, then there will be no use for data.dat, correct?
    #4  
Old Unix and Linux 05-15-2010
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 29 May 2015, 2:09 AM EDT
Location: Zürich
Posts: 8,556
Thanks: 337
Thanked 1,078 Times in 936 Posts
Based on the script you posted, correct.
Sponsored Links
    #5  
Old Unix and Linux 05-15-2010
cosmologist cosmologist is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 28 June 2013, 3:19 PM EDT
Posts: 173
Thanks: 39
Thanked 0 Times in 0 Posts
So if I have this code:


Code:
awk -f get.result.awk data.dat > getrusult.e
./getresult.e

where the get.result.awk is:

Code:
{ name = $1}


{printf("grep %s All.dat | sort -n -k 2.1 | head -1 >>Minimum.dat\n", name)}

here the data.dat file is just indicating the number of lines in the output file. All the columns are coming from All.dat. Correct? (Note: All.dat has more lines than data.dat)
Sponsored Links
    #6  
Old Unix and Linux 05-15-2010
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 29 May 2015, 2:09 AM EDT
Location: Zürich
Posts: 8,556
Thanks: 337
Thanked 1,078 Times in 936 Posts
Wrong.

All.dat is part of your printf statement - not a file read by awk.

If you want to read from All.dat with a view to using it aside from the input file (data.dat), an example would be:


Code:
/Users/scott/scripts/tmp/awk $ cat file1
1
2
3
/Users/scott/scripts/tmp/awk $ cat file2
a
b
c
/Users/scott/scripts/tmp/awk $ awk '{getline x < "file2"; print x}' file1
a
b
c

Awk reads one or more input files and performs actions on each line of those files.

Whatever other files your awk is reading (i.e. using getline) awk will only perform the defined actions for each line of your input file(s).

(in any case, your questions are little to do with the -f option. That option is used to specify an awk program, and has nothing to do with input files)

Last edited by Scott; 05-15-2010 at 07:11 PM..
Sponsored Links
    #7  
Old Unix and Linux 05-15-2010
cosmologist cosmologist is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 28 June 2013, 3:19 PM EDT
Posts: 173
Thanks: 39
Thanked 0 Times in 0 Posts
I am still confused Linux

So what is the role of data.dat in the script I described above ?
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Explain How ":=" works in shell scrpting muddasani Shell Programming and Scripting 2 02-16-2010 06:57 AM
"ld.so.1" error using a cron job | works fine otherwise singh Shell Programming and Scripting 1 08-25-2009 10:33 AM
[Solaris 10]NIS works only from "su" Tex-Twil Solaris 8 04-07-2009 05:02 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM
"kill -14 pid" doesn't works on all processes !! poojac UNIX for Advanced & Expert Users 1 06-23-2005 08:14 AM



All times are GMT -4. The time now is 02:13 AM.