Convert simple ascii table to CSV

10,846

Solution 1

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}'

Solution 2

One easy way to do this is to import your output into Microsoft Excel. First, save the above file as "data.txt". Open Microsoft Excel, Choose File -> Open, then select "data.txt" as the file. You will be prompted by a dialog box which looks like this:

enter image description here

Choose Finish immediately. I tested this with your data above, and using a "Fixed width" delimeter worked well.

You should now see an Excel Workbook with your data neatly in columns. The last step is to save this file in CSV format. Choose "Save As" or press ALT + F + A, then choose the CSV format from the list of extensions, then save as "data.csv". You can verify that everything has worked by opening up "data.csv" in an editor and inspecting.

Note that you may have to do some tidying up in Excel with your column headers, but other than that doing your conversion should be a breeze using Excel!

Share:
10,846
Daryl
Author by

Daryl

Updated on June 04, 2022

Comments

  • Daryl
    Daryl almost 2 years

    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