I have a file f1.txt which has contents like below

file1 file2
file3 file4

and file1 has contents like below

student1 10
student2 20

Now I want to get the first column from f1.txt and print out contents of file1 using awk. I tried awk '{print $1}' f1.txt | cat $1, but it doesn't work. Any simple way to get the contents of file1 using awk?


Within awk you can call function system to run external command:

awk '{system("cat "$1)}' f1.txt

Or if you want to print only content of the file1 and not file3:

awk 'NR==1{system("cat "$1)}' f1.txt

If you are not doing any processing of the data within awk, and using awk just to select the data, this is suited to xargs, e.g.,

awk '{ print $1; }' | xargs cat

That has the advantage over system that xargs will collect multiple parameters for cat, and thus be a little more efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.