Skip to main content
removed the extra character from the prompt: it's better to use the standard $ here for clarity
Source Link
terdon
  • 252.9k
  • 69
  • 481
  • 720

measurements.awk:

BEGIN{FS=";";OFS=","}
{
    for (X=3;X<=NF;X++){
        if (NR==1){name[X]=$X}
        print $1,$2,$X > name[X]".csv"
    }
}

See that NR==1 is only true for the header. So we save the measurements names in the array name, and then proceed to print each set [time,area,measurement] in the corresponding file.

I$$ gawk -f measurements.awk file
I$$ cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
I$$ cat diameter.csv 
time,area,diameter
1,2,5
6,7,10
I$$ cat temperature.csv 
time,area,temperature
1,2,3
6,7,8
I$$ cat pumpmotor.csv 
time,area,pumpmotor
1,2,4
6,7,9

measurements.awk:

BEGIN{FS=";";OFS=","}
{
    for (X=3;X<=NF;X++){
        if (NR==1){name[X]=$X}
        print $1,$2,$X > name[X]".csv"
    }
}

See that NR==1 is only true for the header. So we save the measurements names in the array name, and then proceed to print each set [time,area,measurement] in the corresponding file.

I$ gawk -f measurements.awk file
I$ cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
I$ cat diameter.csv 
time,area,diameter
1,2,5
6,7,10
I$ cat temperature.csv 
time,area,temperature
1,2,3
6,7,8
I$ cat pumpmotor.csv 
time,area,pumpmotor
1,2,4
6,7,9

measurements.awk:

BEGIN{FS=";";OFS=","}
{
    for (X=3;X<=NF;X++){
        if (NR==1){name[X]=$X}
        print $1,$2,$X > name[X]".csv"
    }
}

See that NR==1 is only true for the header. So we save the measurements names in the array name, and then proceed to print each set [time,area,measurement] in the corresponding file.

$ gawk -f measurements.awk file
$ cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
$ cat diameter.csv 
time,area,diameter
1,2,5
6,7,10
$ cat temperature.csv 
time,area,temperature
1,2,3
6,7,8
$ cat pumpmotor.csv 
time,area,pumpmotor
1,2,4
6,7,9
Output had been copied wrong
Source Link
Quasímodo
  • 19.4k
  • 4
  • 42
  • 78

measurements.awk:

BEGIN{FS=";";OFS=","}
{
    for (X=3;X<=NF;X++){
        if (NR==1){name[X]=$X}
        print $1,$2,$X > name[X]".csv"
    }
}

See that NR==1 is only true for the header. So we save the measurements names in the array name, and then proceed to print each set [time,area,measurement] in the corresponding file.

I$ gawk -f measurements.awk file
I$ cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
I$ cat diameter.csv 
time,area,diameter
1,2,5
6,7,10
I$ cat temperature.csv 
time,area,temperature
1,2,3
6,7,8
I$ cat pumpmotor.csv 
time,area,pumpmotor
1,2,4
6,7,9

measurements.awk:

BEGIN{FS=";";OFS=","}
{
    for (X=3;X<=NF;X++){
        if (NR==1){name[X]=$X}
        print $1,$2,$X > name[X]".csv"
    }
}

See that NR==1 is only true for the header. So we save the measurements names in the array name, and then proceed to print each set [time,area,measurement] in the corresponding file.

I$ gawk -f measurements.awk file
I$ cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
I$ cat diameter.csv 
time,area,diameter
1,2,5
I$ cat temperature.csv 
time,area,temperature
1,2,3
I$ cat pumpmotor.csv 
time,area,pumpmotor
1,2,4

measurements.awk:

BEGIN{FS=";";OFS=","}
{
    for (X=3;X<=NF;X++){
        if (NR==1){name[X]=$X}
        print $1,$2,$X > name[X]".csv"
    }
}

See that NR==1 is only true for the header. So we save the measurements names in the array name, and then proceed to print each set [time,area,measurement] in the corresponding file.

I$ gawk -f measurements.awk file
I$ cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
I$ cat diameter.csv 
time,area,diameter
1,2,5
6,7,10
I$ cat temperature.csv 
time,area,temperature
1,2,3
6,7,8
I$ cat pumpmotor.csv 
time,area,pumpmotor
1,2,4
6,7,9
Source Link
Quasímodo
  • 19.4k
  • 4
  • 42
  • 78

measurements.awk:

BEGIN{FS=";";OFS=","}
{
    for (X=3;X<=NF;X++){
        if (NR==1){name[X]=$X}
        print $1,$2,$X > name[X]".csv"
    }
}

See that NR==1 is only true for the header. So we save the measurements names in the array name, and then proceed to print each set [time,area,measurement] in the corresponding file.

I$ gawk -f measurements.awk file
I$ cat file
time;area;temperature;pumpmotor;diameter
1;2;3;4;5
6;7;8;9;10
I$ cat diameter.csv 
time,area,diameter
1,2,5
I$ cat temperature.csv 
time,area,temperature
1,2,3
I$ cat pumpmotor.csv 
time,area,pumpmotor
1,2,4