Reputation: 10039
Is there a way to print filename of "empty" file in a awk.
trying to reply to another awk question that need peer of file (1 file + same filename + extension) i create several file with touch (so empty one). Let's say:
file1.raw
file1.raw.ext
file2.raw
file2.raw.ext
I than use the code
awk 'FNR==1{print FILENAME;print FILENAME".ext"};#some other action on line' file*.raw
result is empty (right, FNR is never 1). how can a print files name when they are treated ? is there a FNR==0
or BEGINF
? I can use argument list and compare but this will be treated asynchronously (BEGIN or END of FNR==1 of a file having at least 1 line)
expected result
file1.raw
(some action result here if file1.raw was not empty)
file1.raw.ext
file2.raw
(some action result here if file2.raw was not empty)
file2.raw.ext
Upvotes: 1
Views: 219
Reputation: 203324
idk why some moderator (@ChrisF) deleted this first time around but this IS the correct answer to this question so let's try again and if that person has a question or comment then perhaps they can post it like a reasonable person would instead of erroneously deleting the correct answer:
with GNU awk:
awk 'BEGINFILE{print FILENAME}1' *
with other awks (untested):
awk '
FNR==1 {
for (++ARGIND;ARGV[ARGIND]!=FILENAME;ARGIND++) {
print ARGV[ARGIND]
}
print FILENAME
}
{ print }
END {
for (++ARGIND;ARGIND in ARGV;ARGIND++) {
print ARGV[ARGIND]
}
}' *
Upvotes: 1