Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk for loop increment value

Reformating files to load into database. Details of files are given below the code.

What I have so far. Everything works except for calculating the hour.

awk 'BEGIN{ FS="|" ; OFS="\t" };
   { for (i = 4; i < NF; i=i+2 ) {          
# +2 Because need to walk row in pairs of QC/Value(s)
       if ( NF == 52 )  {   

            hour = (i - 2)/2  
# Need the value of i, not what is stored in position i.

            qualitycode = i     
            value = i + 1
            print ( $1,$2,$3,$hour,$qualitycode,$value )
        } else {
            print ( "ERROR",$NR,$0 )        
        }
    }
}' $origfile > $tempfile

cat $tempfile | grep ERROR > $errfile

cat $tempfile | grep -v ERROR > $newfile

How can I get the value of i instead of what is stored "in" position i?

In case you're interested.

Original data files are in this format:

Module|Sensor|Date|QC1|Value1|QC2|Value2|QC3|Value3|......|QC23|Value23|QC24|Value24|
90123|PQRST|20161015|4|12.45|4|11.23|4|10.40|4|9.89|......|4|21.36|4|20.55|
65432|BCDEF|20161015|4|6.45|4|7.51|2|9.01|4|11.74|.....|4|18.92|4|16.4|
.....many more rows

There is more than one module and each module has more than one sensor.

Would like to reformat to load into database:

Module\tSensor\tDate\tHour\tQC1\tValue1   
Module\tSensor\tDate\tHour\tQC2\tValue2   
Module\tSensor\tDate\tHour\tQC24\tValue24

Hour would of course need to increment from 1 to 24 for each module/sensor/day?

like image 598
PartTimer Avatar asked Jul 18 '26 19:07

PartTimer


1 Answers

I think you've made a mistake in the print statement:

print $hour

This will de-reference the value in hour, grabbing the value of the field in the position stored in hour, whereas

print hour

will print the actual value in the variable hour.

like image 178
Dan Avatar answered Jul 20 '26 18:07

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!