1

I have written an .awk which executes some operation on a .tr file and writes the output to a file. The END section of .awk file prints this:

printf("%15.2f\n%15.5f\n%15.2f\n%15.2f\n%15.2f\n%10.2f\n%10.2f\n%10.5f\n", rThroughput, rAverageDelay, nSentPackets, nReceivedPackets, nDropPackets, rPacketDeliveryRatio, rPacketDropRatio,rTime) ;

printf("%15.5f\n%15.5f\n%15.5f\n%15.5f\n%15.0f\n%15.9f\n", total_energy_consumption, avg_energy_per_bit, avg_energy_per_byte, avg_energy_per_packet, total_retransmit,rEnergyEfficiency);

I call this .awk file from a .shfile. After executing the command which runs the .awk file, I iterate over the values generated by the .awk file.

        awk -f Wireless_udp.awk 802_11.tr > "TEMP"

        while read val
        do
            l=$(($l + 1))

            if [ "$l" == "1" ]; then
                thr=$(echo "scale=5; $thr+$val/$iteration_float" | bc)
#               echo -ne "throughput: $val " 
            elif [ "$l" == "2" ]; then
                del=$(echo "scale=5; $del+$val/$iteration_float" | bc)
#               echo -ne "delay: $val " 
            elif [ "$l" == "3" ]; then
                s_packet=$(echo "scale=5; $s_packet+$val/$iteration_float" | bc)
#               echo -ne "send packet: $val " 
            elif [ "$l" == "4" ]; then
                r_packet=$(echo "scale=5; $r_packet+$val/$iteration_float" | bc)
#               echo -ne "received packet: $val " 
            elif [ "$l" == "5" ]; then
                d_packet=$(echo "scale=5; $d_packet+$val/$iteration_float" | bc)
#               echo -ne ;"drop packet: $val " 
            elif [ "$l" == "6" ]; then
                del_ratio=$(echo "scale=5; $del_ratio+$val/$iteration_float" | bc)
#               echo -ne "delivery ratio: $val " 
            elif [ "$l" == "7" ]; then
                dr_ratio=$(echo "scale=5; $dr_ratio+$val/$iteration_float" | bc)
#               echo -ne "drop ratio: $val "
            elif [ "$l" == "8" ]; then
                time=$(echo "scale=5; $time+$val/$iteration_float" | bc)
#               echo -ne "time: $val " 
            elif [ "$l" == "9" ]; then
                t_energy=$(echo "scale=5; $t_energy+$val/$iteration_float" | bc)
#               echo -ne "total_energy: $val "
            elif [ "$l" == "10" ]; then
                energy_bit=$(echo "scale=5; $energy_bit+$val/$iteration_float" | bc)
#               echo -ne "energy_bit: $val "
            elif [ "$l" == "11" ]; then
                energy_byte=$(echo "scale=5; $energy_byte+$val/$iteration_float" | bc)
#               echo -ne "energy_byte: $val " 
            elif [ "$l" == "12" ]; then
                energy_packet=$(echo "scale=5; $energy_packet+$val/$iteration_float" | bc)
#               echo -ne "energy_packet: $val " 
            elif [ "$l" == "13" ]; then
                total_retransmit=$(echo "scale=5; $total_retransmit+$val/$iteration_float" | bc)
#               echo -ne "total_retrnsmit: $val \n"
            elif [ "$l" == "14" ]; then
                energy_efficiency=$(echo "scale=9; $energy_efficiency+$val/$iteration_float" | bc)
                #       echo -ne "energy_efficiency: "
            fi
#           echo "$val"
        done < "TEMP"

Everything was running fine, but when I added the last if-else condition and executed the script, it gave a (standard_in) 1: syntax error

Specifically, I'm taking about this segment of code:

            elif [ "$l" == "14" ]; then
                energy_efficiency=$(echo "scale=9; $energy_efficiency+$val/$iteration_float" | bc)
#               echo -ne "energy_efficiency: "

TEMP file contains:

      197645.74
        0.32776
       25000.00
        7350.00
       17348.00
     29.40
     69.39
  24.99826
      720.13300
        0.00015
        0.00117
        0.09798
              0
    0.001166018

I'm having difficulty why it's giving an error.

Link to full code:

.tcl file which generates the .tr file .awk file: .sh file:

bash --version gives:

GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

OS: Ubuntu 16.04 LTS

  • Is the variable energy_efficiency initialized anywhere? if not, the first time through the loop will have an empty value before the + – steeldriver Jan 5 '19 at 1:19
  • Yes, turns out I forgot to initialize the variable. @steeldriver – Robur_131 Jan 5 '19 at 1:24
  • You can give an answer. I will be happy to accept it. – Robur_131 Jan 5 '19 at 1:25
2

Your script doesn't appear to initialize the variable energy_efficiency, so the first time around the loop

echo "scale=9; $energy_efficiency+$val/$iteration_float"

produces

scale=9; +197645.74/1.0

which is a syntax error (bc apparently allows unary -, but not unary +)

$ echo "scale=9; +197645.74/1.0" | bc
(standard_in) 1: syntax error
| improve this answer | |

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.