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? |
|
When you say inside a script ... are you talking inside an awk script or inside a shell script?
|
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 |
to read one line from a file, use
Code:
getline < "file" Code:
awk 'BEGIN{ |
got it!
thanks all... I want to read a file inside an awk script, and I used "getline < file", it works. |
You do also realise you can pass as many file names as you like directly to your script?
Code:
awk '<do stuff>' file1 file2 ... |
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 Should work for more than one file, but I haven't tried that. |
All times are GMT -5. The time now is 09:54 PM. |