grouping lines

Hi all,

I have input lines like below:

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:

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

Any ideas please??

Thx

I made assumption that you want longest value in field #4 and that you want to add numbers in fields #5, #6, #7:

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 FS f3 FS f4 FS f5 FS f6 FS f7}' file
1 Like

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

I made the assumption you want to keep the last description end there are groups of 3 lines with empty lines in between:

awk 'BEGIN{OFS=FS=";";RS=""} {print $1,$2,$3,$18,$5+$12+$19,$6+$13+$20,$7+$14+$21}' infile

Result:

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

thanks bartus11 ,

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

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