LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-13-2011, 11:07 PM   #1
NabiVakili
LQ Newbie
 
Registered: Feb 2011
Posts: 13

Rep: Reputation: 6
How to read a file inside AWK?


I want to read content of a file inside a gawk script.
I know that by using "gawk -f filename" I can read a file, but I want to do that inside the script.

How can I do that?
 
Old 04-13-2011, 11:33 PM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
From google:"gawk read file":

http://web.mit.edu/gnu/doc/html/gawk_5.html#SEC29

getline < file
 
Old 04-14-2011, 02:38 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
When you say inside a script ... are you talking inside an awk script or inside a shell script?
 
Old 04-14-2011, 02:51 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Look at the manpage of awk. "awk -f awkscript"
The filename argument after the -f option is the awk script to run, not the file or files to scan.

Awk reads the file(s) in the argument, or from the pipe input. That is how it works. If you need to explicitly read a lines from a file, look at "getline()" in the gawk info file. This is an advanced usage that you probably don't need.

I'd highly recommend downloading the pdf version of the gawk manual "Gawk: Effective Awk Programming": www.gnu.org/software/gawk/manual/gawk.pdf

Last edited by jschiwal; 04-14-2011 at 02:56 AM.
 
1 members found this post helpful.
Old 04-14-2011, 03:18 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
to read one line from a file, use
Code:
getline < "file"
to read a file from inside awk
Code:
awk 'BEGIN{
  while(( getline line<"file") > 0 ) {
     print line
  }
}
 
1 members found this post helpful.
Old 04-15-2011, 11:01 PM   #6
NabiVakili
LQ Newbie
 
Registered: Feb 2011
Posts: 13

Original Poster
Rep: Reputation: 6
got it!
thanks all...

I want to read a file inside an awk script, and I used "getline < file", it works.
 
Old 04-16-2011, 02:22 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
You do also realise you can pass as many file names as you like directly to your script?
Code:
awk '<do stuff>' file1 file2 ...
 
Old 04-17-2011, 03:44 PM   #8
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 248

Rep: Reputation: 107Reputation: 107
When I needed to write self-contained AWK script that would process the same file each time it was invoked (say, from /proc filesystem), I did the following:

Code:
#!/usr/bin/awk -f

BEGIN {
    # ARGV[0] is the filename of the script itself.
    ARGV[1] = "/path/to/file"
    # Set ARGV length.
    ARGC = 2
}
This way it behaves as if the file was provided as command line argument.

Should work for more than one file, but I haven't tried that.
 
  


Reply

Tags
awk, gawk, getline


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
awk inside makefile RudraB Programming 2 12-18-2009 11:51 AM
Sed inside awk ZAMO Linux - General 1 02-26-2009 04:13 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
awk inside a makefile linux.fob Programming 2 10-12-2005 04:57 PM


All times are GMT -5. The time now is 07:44 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration