I have two text files. The first "file1.txt" has content:
Apple
Orange
Banana
while the second file "file2.txt" has content:
monday
tuesday
wednesday
I want to combine them into one file and its output is:
Apple File1.txt
Orange File1.txt
Banana File1.txt
monday File2.txt
tuesday File2.txt
wednesday File2.txt
Best Answer
That's quite trivial with
awk
:If you want a tab rather than a space between the input line and the filename, add
-v OFS='\t'
to the command-line to set the Output Field Separator (OFS):or use:
That's assuming file names don't contain
=
characters. If you can't guarantee that the filenames won't contain=
characters, you can change that to:Though with GNU
awk
at least, you'd then get warnings if the name of the file contained bytes not forming valid characters (which you could work around by fixing the locale toC
(withLC_ALL=C awk...
) though that would also have the side effect of potentially changing the language of other error messages if any).