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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-13-2011, 11:07 PM
|
#1
|
LQ Newbie
Registered: Feb 2011
Posts: 13
Rep:
|
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?
|
|
|
04-13-2011, 11:33 PM
|
#2
|
Member
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Rep:
|
|
|
|
04-14-2011, 02:38 AM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
When you say inside a script ... are you talking inside an awk script or inside a shell script?
|
|
|
04-14-2011, 02:51 AM
|
#4
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
04-14-2011, 03:18 AM
|
#5
|
Member
Registered: Apr 2010
Posts: 228
Rep:
|
to read one line from a file, use
to read a file from inside awk
Code:
awk 'BEGIN{
while(( getline line<"file") > 0 ) {
print line
}
}
|
|
2 members found this post helpful.
|
04-15-2011, 11:01 PM
|
#6
|
LQ Newbie
Registered: Feb 2011
Posts: 13
Original Poster
Rep:
|
got it!
thanks all...
I want to read a file inside an awk script, and I used "getline < file", it works.
|
|
|
04-16-2011, 02:22 AM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
You do also realise you can pass as many file names as you like directly to your script?
Code:
awk '<do stuff>' file1 file2 ...
|
|
|
04-17-2011, 03:44 PM
|
#8
|
Member
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 388
|
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.
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 12:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|