0

My input file:

000000000 vélIstine IOBAN 00000004960
000000000 shankargu kumar 00000000040
TTTTTTTTT                     0000000200000000050000000000000000000000

whenever I have non Ascii character in the file like above,

my below code snippet not calculating the sum (d_amt_sum+=substr($0,27,10)) properly sometimes its skiping that row and sometime its giving incorrect value instead of 496 its returning 49 for substr($0,27,10)?

besides I want to know how to add print statement inside AWK, example i need to print the value of "substr($0,27,10)" inside the if block how to do that?

 set -A out_result -- `LC_ALL=en_US.UTF-8 awk 'BEGIN{
    d_amt_sum=d_rec_count=d_trailer_out_amt_sum=d_trailer_rec_count=0;
}
{
    if(substr($0,1,9) != "TTTTTTTTT")
    {
            d_amt_sum+=substr($0,27,10); d_rec_count+=1
    }
            else if(substr($0,1,9) == "TTTTTTTTT")
            {
                    d_trailer_out_amt_sum+=substr($0,39,12);
                    d_trailer_rec_count+=substr($0,31,8);
            }
}
END{print d_amt_sum, d_rec_count,d_trailer_out_amt_sum,d_trailer_rec_count}' ${OUTDIR}/${OUT_FILE}

 Expected output 
500,2,500,2
7
  • Non ascii characters like vél Commented Jun 30, 2020 at 20:16
  • Please add your desired output (no description) for that sample input to your question (no comment). Commented Jun 30, 2020 at 20:26
  • Hi am expecting d_amt_sum+=substr($0,27,10) should return 0000000496 from the first line and 0000000004 from second line.. at the end am expecting output d_amt_sum as 500 Commented Jun 30, 2020 at 20:28
  • Also please tell me how to use print statements inside awk.. I want to print and see the value of substr($0,27,10) inside if block.. Commented Jun 30, 2020 at 20:34
  • when will this if(substr($0,0,1) == "TTTTTTTTT") be true? Commented Jun 30, 2020 at 20:38

1 Answer 1

2

you have a logic error on the ordering of the if/else statements, another error on checking 1 char length against 9 char length. Fixing both gives...

awk '{k=substr($0,1,9)
      if(k=="TTTTTTTTT")
         {d_trailer_out_amt_sum+=substr($0,39,12)
          d_trailer_rec_count+=substr($0,31,8)}
      else if(k!="999999999")
         {d_amt_sum+=substr($0,27,10); 
          d_rec_count+=1}}
  END {print d_amt_sum, d_rec_count,d_trailer_out_amt_sum,d_trailer_rec_count}' file

500 2 500 2
Sign up to request clarification or add additional context in comments.

3 Comments

Hi thanks for responding.. how can we print k value inside else if for every iteration
just add print k
@jcrshankar you already had code that does print d_amt_sum and then you asked how to print the value of substr($0,27,10) and I said print substr($0,27,10). Then you asked how to print the value of k. What part of the puzzle are we missing that meant you couldn't figure out print k was how to print the value of k given that you already knew how to print the values of d_amt_sum and substr($0,27,10)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.