Here's one way using gawk. Run like:
awk -f script.awk file
Contents of script.awk:
BEGIN {
FS=OFS=";"
}
NR==1 {
r = $2 FS $5
next
}
{
!x[$1]
a[$2,$5][$1]=$3
}
END {
m = asorti(x,y)
for (k=1;k<=m;k++) {
r = r FS y[k]
}
print r
n = asorti(a,b)
for (i=1;i<=n;i++) {
for (j=1;j<=m;j++) {
for (k in a[b[i]]) {
if (k == y[j]) {
var = a[b[i]][k]
}
}
line = line FS var
var = ""
}
sub(SUBSEP, FS, b[i])
print b[i] line
line = ""
}
}
Alternatively, here's the one liner:
awk 'BEGIN { FS=OFS=";" } NR==1 { r = $2 FS $5; next } { !x[$1]; a[$2,$5][$1]=$3 } END { m = asorti(x,y); for (k=1;k<=m;k++) { r = r FS y[k] } print r; n = asorti(a,b); for (i=1;i<=n;i++) { for (j=1;j<=m;j++) { for (k in a[b[i]]) { if (k == y[j]) { var = a[b[i]][k] } } line = line FS var; var = "" } sub(SUBSEP, FS, b[i]); print b[i] line; line = "" } }' file
Results:
TimeString;Time_ms;A;B;C;D
23.11.201215:03:53;41236627696,7593;1;;2;3
23.11.201215:04:53;41236628391,2037;31;12;1;
23.11.201215:05:53;41236629097,2222;;7;15;8
You need to run dos2unix on your file first. i.e:
dos2unix Flussi0.csv
Alternatively, change the record separator to \r\n so that awk knows what a windows newline ending looks like. You can do this in the BEGIN block:
BEGIN {
FS=OFS=";"
RS="\r\n"
}
Results with the input file posted in the comments below:
"TimeString";"Time_ms";"FIT01";"FIT02";"FIT03";"FIT04";"FIT05";"FIT06"
"22.06.2012 09:31:33";41082396909,7222;1,157408E-02;5,787041E-03;2,507718E-02;2,89352E-03;2,314816E-02;5,787035E-04
"22.06.2012 09:32:34";41082397615,7407;1,157408E-02;5,787041E-03;2,314816E-02;2,89352E-03;2,713479E-02;5,787035E-04
"22.06.2012 09:33:35";41082398321,7593;1,157408E-02;5,787041E-03;2,314816E-02;2,89352E-03;2,314816E-02;5,787035E-04
"22.06.2012 09:34:35";41082399016,2037;1,157408E-02;5,787041E-03;2,314816E-02;2,89352E-03;2,535274E-02;5,787035E-04
"22.06.2012 09:35:36";41082399722,2222;;;;;2,314816E-02;
And this is my desired output:2nd line I see anAvalue of31.Where does this come from? How does that derive from your example? - sorry for the noise - I've got it