How to use awk to display file like cat command

I know cat filename will show contains of the file. How can I imitate such model within awk? What I need is display file contains in BEGIN { .. } awk block. Is that possible? How do we cat a file from awk and display it on screen?

Try using awk’s system():

awk 'BEING {  system("cat /path/to/filename") } { ... } END { ...}' input

Another option is as follows to cat file using awk:

awk 'BEGIN {while ((getline<"/path/to/filename") > 0) {print}}'
1 Like

I made a little change. :smile:

awk 'BEING {template="temp.html";  system("cat " template) } { ... } END { ...}' input

You can check man awk :wink: For simple print in the console: awk '{print}' /path/bla/bla/text.tx
or you can check here: https://github.com/nu11secur1ty/Linux_Deployment_Administration_Hacks-Programing/tree/master/AWK :wink:
BR =)


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki