String concatenation problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String concatenation problem
# 1  
Old 12-09-2012
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:
Code:
1 2
3 4
5 6
7 8
9 10

and my awk code is:
Code:
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!
# 2  
Old 12-09-2012
You can simply use printf like this:

Code:
awk '{printf $1$2}' file

# 3  
Old 12-09-2012
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:

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

# 4  
Old 12-09-2012
Can you post realistic input an desired output files?
# 5  
Old 12-09-2012
The input file is just:

Code:
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:

Code:
 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.
# 6  
Old 12-09-2012
Something like this maybe?

Code:
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:

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

# 7  
Old 12-09-2012
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>.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with String concatenation

I have a script which is migrated from AIX to Linux & now while running it is no able to concatenate string values The string concatenation step under while loop is not displaying desired result Please find below the piece of code: while read EXT_FILE ; do EXT_FILE=$EXT_FILE.ext.sent echo... (7 Replies)
Discussion started by: PreetArul
7 Replies

2. Shell Programming and Scripting

Group by and string concatenation

Hi, I was trying to work on a file which had the following data format 1 hi 1 this 1 is 1 john 2 hello 3 test 3 case the expected output file is the below 1 hi, this, is, john 2 hello 3 test, case I tried using awk or while read, but I couldnt... (13 Replies)
Discussion started by: karthikbhuvana
13 Replies

3. Shell Programming and Scripting

String variable concatenation through loop problem

Hi Team!! Please can anyone tell me why the following line does not work properly? str3+=$str2 it seems that str3 variable does not keep its value in order to be concatenated in the next iteration! Thus when i print the result of the line above it returns the str2 value What i want to do is to... (8 Replies)
Discussion started by: paladinaeon
8 Replies

4. Shell Programming and Scripting

String concatenation

Hi, I have two files. cat file.txt a b c d cat file1.txt j k l m I need the output as a:j (12 Replies)
Discussion started by: nareshkumar522
12 Replies

5. Shell Programming and Scripting

String / Variable Concatenation

Hi all, I'm trying to build a variable name automatically through a for loop for a script I'm working on, basically I want to build the variables named: $JVM_HOME0 or $JVM_HOME1 so that I can loop through some file copy/deletes and a server restart once completed. With the code below, I get this... (3 Replies)
Discussion started by: hydroponx
3 Replies

6. Shell Programming and Scripting

String/Variable Concatenation

Hello, Trying to concatenate the following using bourne shell: # !/bin/bash # this works in bash shell e.g. get the results I am expecting fnTmp=C$cindex.$station_0.$station_1.$station_3.$ts.tmp # # under !/bin/sh # the results are not the same Any assistance would be... (8 Replies)
Discussion started by: LAVco
8 Replies

7. Shell Programming and Scripting

String Concatenation

Hi All, I need to concatenate the values in the array into a variable. Currently the code is : for (( i=1 ; i <= $minCount ; i++ )) do var="${var}""${sample_file}" done The output is : /tmp/1/tmp/2/tmp/3/tmp/4/tmp/5/tmp/6/tmp/7/tmp/8/tmp/9/tmp/10 I need a space between... (1 Reply)
Discussion started by: sh_kk
1 Replies

8. Shell Programming and Scripting

String concatenation with spaces

Hi, I have a variable $ID=40 and I need to build a string like 40 40 40 40 40 40 so repeating ID 'n' times separated by spaces. Any help? Thanks Sarah (2 Replies)
Discussion started by: f_o_555
2 Replies

9. Shell Programming and Scripting

Help concatenation string and variable

Hello, in my script i have this lines of code in a while cycle: .. let j=i+1 t_prod_$i = `cat myfile.csv | grep world | cut -d ";" -f$j` let i+=1 ... So if i try an echo $t_prod_$i at the end of the cycle i cannot see the right value obtained by `cat myfile.csv | grep world |... (5 Replies)
Discussion started by: drain
5 Replies

10. UNIX for Dummies Questions & Answers

string concatenation

my input file contains thousands of lines like below 234A dept of education 9788 dept of commerce 8677 dept of engineering How do i add a delimeter ':' after FIRST 4 CHARACTERS in a line 234A:dept of education 9788:dept of commerce 8677:dept of engineering (7 Replies)
Discussion started by: systemsb
7 Replies
Login or Register to Ask a Question