Skip to content
Advertisement

splitting the file based on repeatation

experts i have a file as below where first column is repeated 0.0,5.0,10.Now i want to split the third column at repeatation of the first column row and want to arrange the data side by side as below:

 0.0     0.0   0.50000E+00
 5.0     0.0   0.80000E+00
10.0     0.0   0.80000E+00
 0.0     1.0   0.10000E+00
 5.0     1.0   0.90000E+00
10.0     1.0   0.30000E+00
 0.0     2.0   0.90000E+00
 5.0     2.0   0.50000E+00
10.0     2.0   0.60000E+00

so that my final file will be

0.50000E+00  0.10000E+00  0.90000E+00
0.80000E+00  0.90000E+00  0.50000E+00
0.80000E+00  0.30000E+00  0.60000E+00
 

so that my final file will be

Advertisement

Answer

Using GNU awk:

awk '{ map[$1][NR]=$3 } END { PROCINFO["sorted_in"]="@ind_num_asc";for(i in map) { for ( j in map[i]) { printf "%st",map[i][j] } printf "n" } }' file

Process each line and add to a two dimensional array map, with the first space delimited field as the first index and the line number the second. The third field is the value. At the end of processing the file, set the array ordering and then loop through the array printing the values in the format required.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement