String concatenation problem

Hi there,

I'm writing a basic script where I want to make a string of 2 numeric fields from a file, which I have done, but the behavior is rather confusing.

I have a file of random values such as:

1 2
3 4
5 6
7 8
9 10

and my awk code is:

BEGIN { FS = " " }
{ str = str $1 $2 }
END { print str }

This gives me an output of 910, whereas what I would expect is 12345678910.
If I add $2++;$2-- before I do the string concatenation, I get the expected result, but not if I put $1++;$1--. Is there an explanation for this?

Oh, and this is running in bash

Thanks in advance!

You can simply use printf like this:

awk '{printf $1$2}' file

The code was just an example of what I was doing. I actually intend to use the numbers to output a hyperlink so in END I would have something like:

print "http://website.com/page.php?something=" str "&somethingelse=1"

Can you post realistic input an desired output files?

The input file is just:

21.9231 -0.19187
22.15547 -0.19345
22.15402 -0.18957
21.85538 -0.18829
21.85517 -0.18963

and expected output would be something like:

 http://mapwebsite.com/map.php?zoom=10&points=21.9231,-0.19187|22.15547,-0.19345|22.15402,-0.18957|21.85538,-0.18829|21.85517,-0.18963|&center=21,-0.19

I already have the expected result, but only by adding $2++ and $2-- into the script.

Something like this maybe?

URL1="http://mapwebsite.com/map.php?zoom=10&points="
URL2="&center=21,-0.19"
VAR=$(awk '{print $1,$2}' OFS="," ORS="|" file)
echo "${URL1}${VAR}${URL2}" 

---------- Post updated at 10:10 AM ---------- Previous update was at 10:02 AM ----------

Or:

VAR=$(tr ' \n' ',|' < file)

Your awk code snippet works perfectly the way you expect. I suspect there to be <CR> <carriage return> chars in the input file (originating from windows?), so you output 12<CR>34<CR>56<CR>78<CR>910<CR><LF> to screen, leaving only 910 visible. $2++$2-- would modify $2 and thus remove the <CR>.

1 Like

Ahh that explains it!

Thanks :slight_smile: