Convert simple ascii table to CSV

Daryl picture Daryl · Mar 5, 2015 · Viewed 8.3k times · Source

I have gprof output shown below and I want to convert it to CSV format. What's the easiest way to do this from the cmd line? (I have many of these types of files, I need to automate this.)

 %   cumulative   self              self     total
 time   seconds   seconds    calls   s/call   s/call  name
 30.02      2.75     2.75   334408     0.00     0.00  primal_bea_mpp
 23.25      4.88     2.13        5     0.43     0.89  price_out_impl
 22.93      6.98     2.10  6336742     0.00     0.00  replace_weaker_arc
  8.84      7.79     0.81     1679     0.00     0.00  refresh_potential
  4.04      8.16     0.37   334402     0.00     0.00  sort_basket
  3.00      8.44     0.28   334402     0.00     0.00  update_tree
  2.07      8.62     0.19 233720751     0.00     0.00  bea_is_dual_infeasible
  2.07      8.81     0.19  8630892     0.00     0.00  insert_new_arc
  2.02      9.00     0.18   334402     0.00     0.00  primal_iminus
  0.55      9.05     0.05        6     0.01     0.01  flow_cost
  0.55      9.10     0.05        4     0.01     0.02  suspend_impl
  0.33      9.13     0.03        6     0.01     0.01  dual_feasible
  0.22      9.15     0.02        7     0.00     0.00  refresh_neighbour_lists
  0.11      9.16     0.01        6     0.00     0.77  primal_net_simplex
  0.00      9.16     0.00        6     0.00     0.00  primal_feasible
  0.00      9.16     0.00        2     0.00     0.00  resize_prob
  0.00      9.16     0.00        1     0.00     0.00  getfree
  0.00      9.16     0.00        1     0.00     9.16  global_opt
  0.00      9.16     0.00        1     0.00     0.00  primal_start_artificial
  0.00      9.16     0.00        1     0.00     0.00  read_min
  0.00      9.16     0.00        1     0.00     0.00  write_circulations

Answer

Nir Alfasi picture Nir Alfasi · Mar 5, 2015

I haven't worked with gprof but assuming that it outputs something like the example above, you can pipe the output to awk:

gprof | awk -v OFS=',' '{print $1,$2,$3,$4,$5,$6,$7}'

In case you have difficulties piping gprof's output, you can save the output (like the example) into a file, say, file.txt and do:

cat file.txt | awk -v OFS=',' '{print $1,$2,$3,$4,$5,$6,$7}'