-3

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 ?

  • 2
    I think your description is confusing. But the error in your code is clear; you have the -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
  • Your edited code still has the syntactical wrong -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
  • @Janis I don't want to compare I want to assign! (also now corrected) – 3kstc Mar 10 '15 at 3:58
  • 1
    You still didn't fix the -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
1

Generally, to pass variables calculated or extracted by awk to the shell, you'd follow this coding patterns...

shell_var=$( awk '{ ...some processing... ; print awk_var }' )

If you have more than one variable you can do...

eval "$( awk '{ ...some processing... ; print "shell_var1="awk_var1, "shell_var2="awk_var2 }' )"

Explanation of the code: The shell pattern var=$( awk '...' ) will assign whatever the awk process will print to the shell variable. Since you can only assign one value by this means you need a different approach for two or more variable assignments. One approach is to let awk print the whole assignment, say awk '{ print "varname=" value }'; the shell construct $( ... ) will again be expanded to the output of the embedded awk call, in this case an assignment. And that expanded assignment shell command to become active you need the shell instruct to evaluate it; eval is the command that does this. The difference to the first (simple) case is that you can let awk print several assignments (separated by whitespace) and eval all in one go.

I'll give an example with an input and an output variable and some calculation...

number=42
square=$( awk -v NUM="$number" 'BEGIN{ print NUM*NUM }' )
echo "$square"

Hope that clarifies the parameter passing in both directions.

  • is there any possibility if you could integrate the code with my 2nd code please? I'm new to all of this, and also this would help me better understand, please? – 3kstc Mar 10 '15 at 3:27
  • As I said in my comment to your question; I think your description is confusing. And it makes no sense to "integrate" this general answer that I gave on your subject into your "2nd code", since it's incorrect code. What I can do is to give you an explanation of my answer; I'll edit it for that. – Janis Mar 10 '15 at 3:33
  • I already knew this, you didn't answer the question, but thanks anyway. – 3kstc Mar 10 '15 at 4:49
  • 1
    in the eval approach, beware that if the values contain special characters like '";&$()\|<>`... Those would have to be escaped for the shell. – Stéphane Chazelas Mar 10 '15 at 7:06

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.