0

I am dumping datum into a csv file (my_datum_file.csv). The contents are all comma seperated, and I want to find the last instance of the 4th parameter (i.e. the last instance of datum regarding MX3, TMX, MSV, etc. where printed). The 5th parameter is a 'subclass' of the 4th parameter (ie TMX has 098 and 001). It is also important that the 7th parameter be "status" as opposed to "SVlts". Q: I want the 10th value (which is a period of time in minutes) to convert to hours. I would like to print this value out.

So from the terminal I wrote:

$ cat /home/ed/start_up_job/my_datum_file.csv | awk -F, '( $4 == "TMX" ) && ( $5 == "098" ) && ( $7 == "status" ) {print $10/60}' | tail -1`'

The end result is 1022.25 hours, which is great;

I've embedded this into generic little bash script

#!/bin/bash

machine_ID="$1"
machine_number="$2"

echo "$1 $2 hours is `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/my_datum_file.csv`"

so when I call my bash script from the terminal $ ./myscript.txt TMX 098 I get the following output TMX 098 hours is: 0 My expected output would be TMX 098 hours is: 1022.25

I don't know what is wrong?

  • 1
    Your $machine_ID and $machine_number shell variables are not going to get expanded in the awk expression (because of the single-quoting). Instead, look can use -v to pass their values to awk e.g. awk -F, -vid="$machine_id" '$4 == id ...' and so on. – steeldriver Mar 5 '15 at 1:09
  • @steeldriver how do i seperate variable assignments? awk -F, -vMachine_ID="$machine_ID", -vMachine_Number=$machine_number '($4 == Machine_ID) && ($5 == Machine_number ) && $7 == 'status" {print $10/60}' is this right? – 3kstc Mar 5 '15 at 1:17
2

Replacing cat that was used from the terminal and with 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/my_datum_file.csv worked.

Understanding the code:

  • awk -F defines the field seperator as ,
  • -v assigns a value to a program variable
  • MID="$machine_ID" where as MID is the newly given variable name within awk for $machine_ID, which is a shell variable
  • -v MNR="$machine_number" similarly -v reassigns a awk variable called asMNR for a shell variable called $machine_number
  • ( $4 == MID ) equates the 4th parameter with what MID holds
  • && and
  • ( $5 == MNR ) equates the 5th parameter with what MNR holds
  • && and
  • ( $7 == "status" ) equates the 7th parameter with status
  • {t=$10} assigns the 10th value to t
  • END is rule that executed, once, after all the input has been read
  • print int(t/60) print the output of t/60 (note no $ sign infront of t) as an integer
  • /home/ed/start_up_job/my_datum_file.csv from the file in ~

Essentially, in the file /home/ed/start_up_job/my_datum_file.csv this bit of code finds the values which hold MID and MNR. Then the code places the value of the [$]10th parameter to t and prints it as an integer after /ing by 60

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.