rahul gulati
rahul gulati

Reputation: 188

AWK Assignment to variable

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?

Upvotes: 1

Views: 123

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 184955

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.

Upvotes: 1

jim mcnamara
jim mcnamara

Reputation: 16379

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.

Upvotes: 0

Related Questions