concatenate consecutive field values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting concatenate consecutive field values
# 1  
Old 07-13-2010
Question concatenate consecutive field values

Hi,

I have a file like this

Code:
A  Bob
A  Sam
A  John
B  David
C  Paul
C  Sandra

If the consecutive field values in column one is same, then concatenate the corresponding strings.

So, I need an output like this,

Code:
A Bob_Sam_John
B David
C Paul_Sandra

I usually work with excel but couldn't make it. Any ideas how I can achieve this with AWK?

thanks in advance.

Last edited by radoulov; 07-13-2010 at 05:08 PM.. Reason: Code tags, please!
# 2  
Old 07-13-2010
Try:
Code:
awk 'p!=$1{if(NR>1)print s;s=$0;p=$1;next}{s=s"_"$2}END{print s}' infile

# 3  
Old 07-13-2010
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk 'p!=$1{if(NR>1)print s;s=$0;p=$1;next}{s=s"_"$2}END{print s}' infile

thanks a lot - its working perfectly.SmilieSmilie
# 4  
Old 07-13-2010
Code:
awk '{a[$1]=a[$1]"_"$2}END{for (i in a) {sub(/^_/,"",a[i]);print i,a[i]}}' urfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - If field value of consecutive records are the identical print portion of lines

I have some data that looks like this: PXD= ZW< 1,6 QR> QRJ== 1,2(5) QR> QRJ== 4,1(2) QR> QRJ== 4,2 QRB= QRB 4,2 QWM QWM 6,2 R<C ZW< 11,2 R<H= R<J= 6,1 R>H XZJ= 1,2(2) R>H XZJ= 2,6(2) R>H XZJ= 4,1(2) R>H XZJ= 6,2 RDP RDP 1,2 What I would like to do is if fields $1 and $2 are... (5 Replies)
Discussion started by: jvoot
5 Replies

2. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

3. Shell Programming and Scripting

Deleting consecutive equal values in a file

Hello everyone, I have a requirement as shown below. I need to delete consecutive same values from the source file and give it as output file. Source: a,b,c,d,e,e,f,g Target: a,b,c,d,f,g The repeating value "e" should be deleted from the file completely. How can I achieve this... (14 Replies)
Discussion started by: vamsikrishna928
14 Replies

4. Shell Programming and Scripting

awk print values between consecutive lines

I have a file in below format: file01.txt TERM TERM TERM ABC 12315 68.53 12042013 165144 ABC 12315 62.12 12042013 165145 ABC 12315 122.36 12052013 165146 ABC 12315 582.18 12052013 165147 ABC 12316 2.36 12052013 165141 ABC 12316 ... (8 Replies)
Discussion started by: alex2005
8 Replies

5. Shell Programming and Scripting

Adding the corresponding values for every 5th consecutive numbers

Dear All, I have a file which is as follows: Input File: 231 100.1 233 99 235 200.9 238 80.1 239 90.2 240 77.0 243 99.5 245 16.20 246 13.55 247 11.8 249 13.7 250 99.6 (1 Reply)
Discussion started by: NamS
1 Replies

6. Shell Programming and Scripting

Concatenate last field values for all occurences

Hello all, Maybe you can help me with an awk script to get what I need. I have the input file with format below: REQUEST|79023787741690|738227864597|985 REQUEST|79024002151717|738229423534|985 REQUEST|79024002151717|738229423534|*985 NDS-REQUEST|79024002151717|738229423534 ... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

7. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies

8. Shell Programming and Scripting

AWK: combining consecutive values in a field

Hi, Here is my sample input X 2 AAA Y 3 BBB Y 2 CCC Z 4 DDD In field 1, if the value of one line is same as that of next line, I want to concatenate the corresponding value of the second line in the third field with the value of the third field of first line. And I dont need the third... (2 Replies)
Discussion started by: polsum
2 Replies

9. Programming

FORTRAN: Getting Consecutive DIST Values

I have this code and I want to get the first two consecutive distances (obtained by calling GET_TH(IT, 'DIST', DIST) at index IT, the result stored in DIST) for which (ITFLG(IT) .NE. 1). L_FIRST get the first DIST for which ITFLG(IT) .NE. 1. Getting a bit confused on how to achieve this. ... (0 Replies)
Discussion started by: kristinu
0 Replies

10. UNIX Desktop Questions & Answers

How to concatenate consecutive lines

I have a few lines like -- feature 1, subfeat 0, type 3, subtype 1, value 0, -- feature 1, subfeat 0, type 1, subtype 1, value 0, I would like to concatenate the... (1 Reply)
Discussion started by: shivi707
1 Replies
Login or Register to Ask a Question