That's quite trivial with awk
:
$ awk '{print $0,FILENAME}' File*.txt
Apple File1.txt
Orange File1.txt
Banana File1.txt
monday File2.txt
tuesday File2.txt
wednesday File2.txt
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):
awk -v OFS='\t' '{print $0,FILENAME}' File*.txt
or use:
awk '{print $0 "\t" FILENAME}' File*.txt
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:
awk '{print $0 "\t" substr(FILENAME, 3)}' ./File*.txt
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 to C
(with LC_ALL=C awk...
) though that would also have the side effect of potentially changing the language of other error messages if any).
--
as most other GNU programs do or have some other way of stopping it from interpreting=
in a filename asvar=value
. – cas Aug 21 '17 at 11:41gawk
does understand--
as end-of-options like all POSIX awk implementations (awk -- 'code' args
) but it doesn't stop arguments with=
from being treated as variable assignments. It hasawk -E file.awk args
for that though (so you can dogawk -e 'code' -E /dev/null other args x=y.txt File*.txt...
). But that's gawk-specific. – Stéphane Chazelas Aug 21 '17 at 17:34