grouping lines


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users grouping lines
# 1  
Old 08-27-2010
grouping lines

Hi all,

I have input lines like below:

Code:
A;100;Paris;City;10;0;0
A;100;Paris;City;0;10;0
A;100;Paris;City in Europe;0;0;20

B;101;London;City;20;0;0
B;101;London;City;0;20;0
B;101;London;City in Europe;0;0;40

I need to group the above lines to:
Code:
A;100;Paris;City in Europe;10;10;20
B;101;London;City in Europe;20;20;40

Any ideas please??

Thx

Last edited by Scott; 08-27-2010 at 05:53 PM.. Reason: Please use code tags
# 2  
Old 08-27-2010
I made assumption that you want longest value in field #4 and that you want to add numbers in fields #5, #6, #7:
Code:
awk -F\; '{if(length(f4[$1])<length($4))f4[$1]=$4;f2[$1]=$2;f3[$1]=$3;f5[$1]+=$5;f6[$1]+=$6;f7[$1]+=$7}
END{for (i in f2) print i FS f2[i] FS f3[i] FS f4[i] FS f5[i] FS f6[i] FS f7[i]}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-27-2010
You could use awk with semi-colon as the field separator. Can't actually write this for you without full info about the data.... I could take guesses... you could post your assignment sheet (betting that this is for school).
# 4  
Old 08-28-2010
I made the assumption you want to keep the last description end there are groups of 3 lines with empty lines in between:
Code:
awk 'BEGIN{OFS=FS=";";RS=""} {print $1,$2,$3,$18,$5+$12+$19,$6+$13+$20,$7+$14+$21}' infile

Result:
Code:
A;100;Paris;City in Europe;10;10;20
B;101;London;City in Europe;20;20;40

# 5  
Old 08-28-2010
thanks bartus11 ,

your solution is fine, but it throws the first line below:

Code:
;;;;0;0;0
A;100;Paris;City in Europe;10;10;20
B;101;London;City in Europe;20;20;40


Last edited by Scott; 08-28-2010 at 06:35 AM.. Reason: Code tags
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 grouping and zipping

Hi can you please help with the below ? source file: Column1,Column2,Column3,Column4 abc,123,dir1/FXX/F19,1 abc,123,dir1/FXX/F20,1 abc,123,dir1/FXX/F23,2 abc,123,dir1/FXX/C25,2 abc,123,dir1/FXX/X25,2 abc,123,dir1/FXX/A23,3 abc,123,dir1/FXX/Z25,3 abc,123,dir1/FXX/Y25,4 I want to... (3 Replies)
Discussion started by: paul1234
3 Replies

2. Shell Programming and Scripting

Grouping and counting

Hello, I would like to group/sort a file of records by a particular field and then count how many records belong in that grouping. For example say I have the following data: 1234|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|AA ABCD|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|AA EFGH|"ZZZ"|"Date"|"1"|"Y"|"ABC"|""|BB... (14 Replies)
Discussion started by: Nik44
14 Replies

3. Shell Programming and Scripting

Name grouping

awk 'FNR==NR {a; next} $NF in a' genes.txt refseq_exons.txt > output.txt I can not figure out how to group the same name in $4 together. Basically, all the SKI together in separate rows and all the TGFB2. Thank you :). chr1 2160133 2161174 SKI chr1 218518675 218520389 TGFB2... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Grouping

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

5. Shell Programming and Scripting

regular expression grouping across multiple lines

cat book.txt book1 price 23 sku 1234 auth Bill book2 sku 1233 price 22 auth John book3 auth Frank price 24 book4 price 25 sku 129 auth Tod import re f = open('book.txt', 'r') text = f.read() f.close() m =... (2 Replies)
Discussion started by: chirish
2 Replies

6. Shell Programming and Scripting

Selective grouping

I have a text file in this format. Group: AAA Notes: IP : 11.11.11.11 #User xxxxxxxxx #Password aaaaaaaaaaaaaaaa Group: AAA Notes: IP : 11.11.11.22 #User yyyyyyyyyyyyy #Password bbbbbbbbbbbbb (8 Replies)
Discussion started by: anil510
8 Replies

7. UNIX for Dummies Questions & Answers

Grouping or appending the lines in a file through Unix

Hi, I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this A 1 100 abc def A 1 200 abc def A 1 300 abc def A 2 100 pqr def A 2 200 pqr def A 2 300 pqr def A 1 100 abc def A 1 200 xyz def A 1 300 xyz def I need it as... (6 Replies)
Discussion started by: mkandula1983
6 Replies

8. Shell Programming and Scripting

Grouping or appending the lines in a file through Unix

Hi, I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this A 1 100 abc def A 1 200 abc def A 1 300 abc def A 2 100 pqr def A 2 200 pqr def A 2 300 pqr def A 1 100 abc def A 1 200 xyz def A 1 300 xyz def I need it as... (1 Reply)
Discussion started by: mkandula1983
1 Replies

9. Shell Programming and Scripting

Grouping each 2 lines

Whats a good way to group (by adding a new integer to the front of each line) pairs of lines, such that lines 1 & 2 are group 1, lines 3 & 4 are group 2, etc... ex input: A B C D etc... ex output: 1A 1B 2C 2D etc... (5 Replies)
Discussion started by: watingo
5 Replies

10. UNIX for Advanced & Expert Users

Regarding the user grouping

I am new to the unix server. My question is: i have 10 users a b c d e f g h i j Among the 10 5 users are (normal) user and remainging 5 users are (qad) users How can i sepearte this one? How can i give the dlc access rights? Please provide the clear cut idea. (0 Replies)
Discussion started by: kingganesh04
0 Replies
Login or Register to Ask a Question

Featured Tech Videos