7

I have a script which generates several output files and use these output files during runtime.

Following are the some of the files generated by the script: apple.txt, section_fruit_out.csv, section_fruit_out_lookup.csv, food_lookup.csv, section_fruit_lookup.csv.

I have a code phrase as below:

nawk 'FNR == NR && NF!=0 {x[$1] = $1; next;} {FS=OFS=","} FNR>1{if ($2 in x) {($6 = "apple")} } 1' apple.txt section_fruit_out.csv > section_fruit_out_lookup.csv 
nawk 'BEGIN { FS = OFS = ","; } FNR == NR { x[$1] = $2; next; } { if ($7 in x && $6 == "") { $6 = x[$7]; } else if ($6 == "" && $7 != "") { $6 = "TO_BE_DEFINED" } } 1' food_lookup.csv section_fruit_out_lookup.csv  > section_fruit_lookup.csv

This code phrase mainly handles the expected job. But the script does not work as expected if the apple.txt file is empty (this file is generated by database queries). If the apple.txt file is empty, the output file (section_fruit_out_lookup.csv) of the first nawk section is also generated empty. Since section_fruit_out_lookup.csv is generated empty and it is used by the second nawk command, the second nawk command also generates an empty output file (section_fruit_lookup.csv).

How can I bypass first nawk command if the apple.txt file is empty and make the second nawk command to use section_fruit_out.csv file instead of using the file: section_fruit_out_lookup.csv?

AdminBee
  • 22,803
Murat
  • 335

2 Answers2

9

Instead of the NR == FNR test to test whether you're processing the first file, you could do:

awk 'FILENAME == ARGV[1] {...} ...' file1 file2

But that's a more expensive test, so if file1 is a regular file, you might as well use @Archemar's approach and not run awk at all if the first file is empty.

In cases (not yours) where file1 and file2 have to be the same file, you can do:

awk 'FILENAME == ARGV[1] {...} ...' file1 ./file1

Or:

awk 'FILENAME == "-" {...} ...' - <file1 file1

An even better approach (portable and efficient):

awk '!file1_processed {...} ...' file1 file1_processed=1 file2

If you need that to apply on ./*.txt for instance, you'd do:

set -- ./*.txt
first=$1; shift
awk '!first_processed {...} ...' "$first" first_processed=1 "$@"

A GNU awk-specific approach:

awk 'ARGIND == 1 {...} ...' file1 file2
7

there is a test function for zero size file.

if test -s apple.txt
then 
    ## apple.txt non empty
    code ...
else
    ## file empty
fi
Archemar
  • 31,554