i'm trying to use awk to format a file thats contains multiple line.
Contains of file:
ABC;0;1
ABC;0;0;10
ABC;0;2
EFG;0;1;15
HIJ;2;8;00
KLM;4;18;12
KLM;6;18;1200
KLM;10;18;14
KLM;1;18;15
result desired:
ABC;0;1;10;2
EFG;0;1;15
HIJ;2;8;00
KLM;4;18;12;1200;14;15
I am using the code below :
awk -F ";" '{
ligne= ligne $0
ma_var = $1
{getline
if($1 != ma_var){
ligne= ligne "\n" $0
}
else {
ligne= ligne";"NF
}
}
}
END {
print ligne
} ' ${FILE_IN} > ${FILE_OUT}
the objectif is to compare the first column of the next line to the first column the current line, if it matches then add the last column of the next line to the current line, and delete the next line, else print the next line.
Kind regards,
awk -F ";" '{if (a[$1]=="") a[$1]=$0; else a[$1]=a[$1]";"$NF} END{for(i in a) print a[i]}' file- Cyrusif (a[$1]=="") a[$1]=$0; else a[$1]=a[$1]";"$NF=a[$1]=(a[$1]=="" ? $0 : a[$1] FS $NF). Just slightly less redundancy. Obviously that approach will read the whole file into memory and will output the lines in random order but maybe that's OK. IMHO you should post it as an answer. - Ed Morton