Reputation: 331
I have the following script:
awk '
# Write 1st file into array
NR == FNR {
array[NR] = $0;
next;
}
# Process 2nd file
{
...
} ' file1 file2
What I want is to write 1st file into array and later use this array in 2nd file. First file may be empty, my problem appears when awk read empty file, it does not execute any user-level awk program code and skip to the second file. When awk is reading 2nd file NR == FNR
is true and the awk program write 2nd file into array.
How I can avoid it, so only first file will be put into array if exist?
Upvotes: 1
Views: 118
Reputation: 784998
Use this condition to safeguard empty file scenarion:
ARGV[1]==FILENAME && FNR==NR {
array[NR] = $0
next
}
ARGV[1]
will be set to first filename in the awk command line and FILENAME
will represent the current filename being processed.
Upvotes: 4