4

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 generated also empty. Since section_fruit_out_lookup.csv generated empty and it is used by the second nawk phrase, the second nawk phrase also generates empty output file (section_fruit_lookup.csv). How can I bypass first nawk phrase if the apple.txt file is empty and make the second nawk phrase to use section_fruit_out.csv file instead of using the file: section_fruit_out_lookup.csv?

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
  • 1
    (note that you can't use that approach if apple.txt is not a regular file) – Stéphane Chazelas Oct 19 '15 at 9:14
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.