Group by and string concatenation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Group by and string concatenation
# 8  
Old 04-09-2013
Hi Yoda,

Here is the sample Input and output

Code:
1  Bill     Portland          USA 
1  Robert Los Angeles   USA 
1  Mike   Amsterdam     Netherlands 
1  Rick   New York        USA 
2  Adam   Houston        USA 
3  Dave   Mexico City   Mexico 
3  Andy   Phoenix         USA

Code:
1  Bill, Robert, Mike, Rick     Portland,Los Angeles,Amsterdam, New York USA,USA, Netherlands, USA 
2  Adam                                  Houston                                                            USA 
3  Dave, Andy                        Mexico City, Phoenix                                       Mexico, USA

# 9  
Old 04-09-2013
Are they in one line? If so, try this. If not, check my previous post.

Code:
 awk '{for(i=1;i<=NF;i++){if($i~/[1-9]/){x=$i;continue}A[x]=(A[x]?A[x]","$i:$i)}}END{for(i in A)print i, A[i]}' infile

Actually, it will work for both. Just than here we have an additional check which can be avoided.

--ahamed
# 10  
Old 04-09-2013
Hi Ahamed,

Thanks for replying, but the output from the command comes as



3 Dave,Mexico,City,Mexico,Andy,Phoenix,USA ( note that the lines are being grouped and appended with a coma, which makes the entire string into a single field)

whereas the output that i want is

3 Dave,Andy Mexico City,Phoenix Mexico,USA ( note the spaces between names city country , which makes it 3 seperate fields similiar to the original layout)
# 11  
Old 04-09-2013
How do you explain Dave,Andy Mexico City,Phoenix Mexico,USA?
Is that there should be no comma between the last city from previous column and the first city in the next column?

Also, is this your actual file? Or will it change later again?

--ahamed
# 12  
Old 04-09-2013
Yes Smilie

---------- Post updated at 05:08 PM ---------- Previous update was at 05:00 PM ----------

Ahamed,


The actual file just contains 2 columns and the awk command provided by Yoda works perfectly fine for it, but I am pretty sure we will have more of such requirements in future , so I just wanted to understand how to deal if we have more fields , which I am sure we will.

Thanks,
Karthik
# 13  
Old 04-09-2013
Try this

Code:
awk '
{
        for(i=3;i<NF;i++){x=x" "$i}
        A[$1]=(A[$1]?A[$1]","$2:$2)
        B[$1]=(B[$1]?B[$1]","x:x);x=""
        C[$1]=(C[$1]?C[$1]","$NF:$NF)
}
END{ for(i in A){print i, A[i], B[i], C[i]} } ' infile

Assumptions are Name and Country is going to be a single string and the patter remains the same i.e. # Name City Country

The script will change based on the patterns!

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 14  
Old 04-10-2013
Thanks Ahamed, this is perfect, i can change the script according to the format i have at hand now.

--Karthik
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

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 {... (7 Replies)
Discussion started by: HMChadwick
7 Replies

3. 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

4. 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

5. Shell Programming and Scripting

String concatenation problems

#! /bin/csh set tt=12345_UMR_BH452_3_2.txt set rr=`echo $tt | cut -d_ -f1` set rr1=welcome set ff=$rr $rr1 echo $ff why $ff returned only 12345 and not 12345welcome? thanks (2 Replies)
Discussion started by: jdsignature88
2 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