1

I am looking for assignment of value of variable to another variable in awk.

Below is the example

if (substr($0,1,1) == "4") 
{
    COUNT_DETAIL_SEG++
    SUM_OF_DETAIL_RCDS=$(COUNT_DETAIL_SEG)
    print "Sum of detail records is" SUM_OF_DETAIL_RCDS
}

Suppose if 1st character of line comes out to be "4" then i want to increment value of COUNT_DETAIL_SEG and assign this value to SUM_OF_DETAIL_RCDS and thereby print it. But i am unable to retain this value. Can anyone have any idea about this?

2 Answers 2

1

I think instead of

SUM_OF_DETAIL_RCDS=$(COUNT_DETAIL_SEG)

you want :

SUM_OF_DETAIL_RCDS=COUNT_DETAIL_SEG

because in , when you put $(INT), you refer no the N'th column of the current input line.

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

1 Comment

Thanks to accept the answer if it fit your needs. That's hos stackoverflow work.
0

Maybe you want to do this at the start of the awk statement. @sputnick is on the right track.

awk -v var="$bash_var" '{ awk code here}'

This will use the awk internal variable var instead of the shell variable bash_var.

Comments

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.