awk - awk concatenate string variable

5

1

I would like to awk concatenate string variable in awk. How can I do that? I tried:

BEGIN{
t="."
r=";"
w=t+r
print w}

But I does't work. Output:

0

Or I want to add variable and result of function. Input:

t t t t
a t a ta
ata ta a a

Script:

{
key="t"
print gsub(key,"")#<-it's work
b=b+gsub(key,"")#<- it's something wrong
}
END{
print b}#<-so this is 0

Output:

4
2
2
0#<-the last print

diego9403

Posted 2015-08-31T07:43:52.663

Reputation: 807

With t+r you implicitly cast both variables to numbers, and both become zero. Strings resembling numbers are converted to numbers: t="1";r="2";w=t+r;print w prints 3. – simlev – 2018-04-12T13:01:23.783

Answers

10

No operator is needed (or used). Your example would be something like

BEGIN{
t="."
r=";"
w=t r
print w}

For related discussion

Thomas Dickey

Posted 2015-08-31T07:43:52.663

Reputation: 6 891

Ok. I have one more problem. I would like to make arithmetic operation: b=b+gsub(key,""). I will add this in my post. – diego9403 – 2015-08-31T08:56:24.937

@diego9403: post it as a new question instead – Thor – 2015-08-31T10:31:07.863

agree - the addition to OP's question exercises a different aspect of strings. – Thomas Dickey – 2015-08-31T19:34:52.457