I've got a live MQTT stream being filtered out and dumped into a CSV file. I use the fourth parameter ($4
) and fifth parameter ($5
) in the CSV file to locate the information I want. I give this information to awk (i.e. $machine_ID
and $machine_number
) for it to locate the latest data which has the parameters I have defined. The code looks like this:
engine_hours=`awk -F, -v MID="$machine_ID" -v MNR="$machine_number" '( $4 == MID ) && ( $5 == MNR ) && ( $7 == "status" ) {t=$10} END{print int(t/60)}' /home/ed/start_up_job/jacon_mqtt.csv`
engine_hours
is sent via MySQL to update a table corresponding to $machine_ID
and $machine_number
. So essentially I choose which machine to update when.
Now, I want $5
and $4
to pass its value to MNR
or $machine_number
and MID
or $machine_ID
respectively. In which I will concatenate them to make the $machine_name
variable.
So the code should be something like:
engine_hours=`awk -F, '( MID = $4 ) && ( MNR = $5 ) && ( $7 == "status" ) -v "$machine_ID"=MID -v "$machine_number"=MNR {t=$10} END{print int(t/60)}' /home/ed/start_up_job/jacon_mqtt.csv`
The error I get is awk: line 1: syntax error at or near =
How can I remedy this ?
-v
variable definitions from shell in the mid if your awk code instead of in front of it as you've had it in the upper piece of code. If you condense what you what to achieve with small samples of input data and expected output data it's easier to also solve your task. – Janis Mar 10 '15 at 3:16-v
expressions in he mid of your awk code. Also note that if you want to compare fields like $4 and $5 against awk-variables MID and MNR you have to assign values to them. (Ignoring your syntax error for a moment) you seem to have that also the wrong way;awk -v MID="$machine_ID" ' ... '
would be the right ordering if assigning shell variables to awk variables. – Janis Mar 10 '15 at 3:55-v
issue. (And it's dubious if you assign in the condition part and catenate the assignments with&&
. You should probably read an awk tutorial before continuing.) – Janis Mar 10 '15 at 4:05