Yes, you can read from multiple files at the same time using awk.
In order to do that, you should use the getline
command to explicitly control input.
In particular, you want to use getline
with a file so that you're not reading from the file(s) passed in as main arguments to awk.
One possibility, in your case:
$ awk '{
getline line < "tab2"
print $0, FS, line
}' tab1
This is not doing much error handling or anything, and as you can notice, the filename is now hardcoded in the awk script. You can fix those issues somehow, at the cost of making the script really uglier.
So, while yes, you can do this from awk
.
(Side note: Whether it's a good idea to do this from awk, that's another story... In my opinion, this starts getting messy really quickly from here, so you're probably better off switching now to a higher level language, such as Python, Perl, Ruby, Lua, etc., as that kind of language will give you much better support for file handles and objects than what you'll get in awk. While bash / shell scripting would do somewhat better in dealing with multiple files, I wouldn't recommend that either, since it doesn't do as good as the aforementioned ones in writing modular maintainable testable code. Just my 2c.)
paste
utility... – steeldriver Aug 12 '19 at 16:19awk
, it seems to me that an answer aboutpaste
would be appropriate. Would you be willing to post one? – Eliah Kagan Aug 12 '19 at 16:52