awk problem - combining awk statements

i have a datafile that has several lines that look like this:

2,dataflow,Sun Mar 17 16:50:01 2013,1363539001,2990,excelsheet,660,mortar,660,4

using the following command:

awk -F, '{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, $DATAFILE | egrep -v "\-OLDISSUES," | ${AWK} "/${MONTH} ${DAY} ${TIMEH}:.*:.*/,0" 

i want the lines to be outputted only if certain fields have numbers that are bigger than set limit.

i was thinking something like this:

awk -F, '{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, $DATAFILE | egrep -v "\-OLDISSUES," | ${AWK} "/${MONTH} ${DAY} ${TIMEH}:.*:.*/,0" | ${AWK} -F"," '{if($9>=11) {print $0} }'

but i feel thats just too much awk. it could all be done with one awk...at least i hope.

Are you sure it's not strftime(...,$4) ? $3 is not an epoch time, but $4 looks like one.

You can combine them certainly:

awk '($9 >= 11) && !/-OLDISSUES/ { ...}'

I can fill things out in more detail if you explain what this does, I'm unfamiliar with this syntax:

${AWK} "/${MONTH} ${DAY} ${TIMEH}:.*:.*/,0"

thanks for responding. field three is actually in epoch time, but when you run the

"'{$3=strftime("%a %b %d %T %Y,%s",$3)}1' OFS=, "

on it, it translate it into what is shown above.

the

${AWK} "/${MONTH} ${DAY} ${TIMEH}:.*:.*/,0"

is to print all lines it finds under the first line that matches "MONTH DAY TIME". for instance, "Mar 19 18:.*:.*".

my problem is that the number of fields in each line in the datafile changes depending on a number.

so while the awk is printing all lines it finds under "Mar 19 18:", i want it to also run condition check. so for each line, i want to do something like this: "if field 9 of this line i'm currently reading is greater than 11", then print this, else, print this." this is where i get lost.

From awk

Since the expressions are evaluated in a boolean context, the 0 will never trigger a match. So the quoted code means that once the regular expression matches, its actions will be applied to the line that matched the regexp and all subsequent lines.

Regards,
Alister

how would you recommend fixing this?

I wasn't pointing out a problem. I was merely explaining how the range worked, in response to Corona's unfamiliarity.

Regards,
Alister

So the ",0" actually does nothing then? He's just matching individual lines with a regex?

Since 0 always yields a boolean false, the range is never closed. It's functionally equivalent to sed's /re/,$

Regards,
Alister

1 Like