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
?
apple.txt
is not a regular file) – Stéphane Chazelas Oct 19 '15 at 09:14