What is wrong with this awk statement?

echo $line | awk -F, "BEGIN {if(NF==10) && /'"${MONTH}"' '"${DAY}"' '"${TIMEH}:"'/,0 {print $10"-"$4} else {print $13"-"$4}}"

if the fields in the line is equal to 10, print the values of this specified fields "$10 and $4".

if the fields in the line is anything but 10, print the values of field "$13 and $4".

Why is it inside BEGIN block?

A BEGIN rule is executed once only, before the first input record is read.

initially, this is the command i was using:

echo $line | awk -F, '(NF==10) && /'"${MONTH}"' '"${DAY}"' '"${TIMEH}:"'/,0 {print $10"-"$4}'

but i needed to include the "else" condition in there.

So, why is it inside the BEGIN block? :slight_smile:

You could have something like:

awk -F, '/'"${MONTH}"' '"${DAY}"' '"${TIMEH}:"'/,0 {if(NF==10) {print $10"-"$4} else {print $13"-"$4}}'
1 Like

In the BEGIN section, no file is read yet, so NF is not defined. Try

echo $line | awk -F -v DateTime="${MONTH} ${DAY} ${TIMEH}" \
    'match ($0, DateTime) {InRange=1}
     InRange && NF==10    {print $10"-"$4; next}
     InRange              {print $13"-"$4}
    '
1 Like

this did the trick. Thank you!!!!!!!!

---------- Post updated at 12:28 PM ---------- Previous update was at 12:24 PM ----------

this too, thanks so much!