Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to transpose a list to a table in bash
    primarykey
    data
    text
    <p>I would like to transpose a list of of items (key/value pairs) into a table format. The solution can be a bash script, awk, sed, or some other method.</p> <p>Suppose I have a long list, such as this:</p> <pre><code>date and time: 2013-02-21 18:18 PM file size: 1283483 bytes key1: value key2: value date and time: 2013-02-21 18:19 PM file size: 1283493 bytes key2: value ... </code></pre> <p>I would like to transpose into a table format with tab or some other separator to look like this:</p> <pre><code>date and time file size key1 key2 2013-02-21 18:18 PM 1283483 bytes value value 2013-02-21 18:19 PM 1283493 bytes value ... </code></pre> <p>or like this:</p> <pre><code>date and time|file size|key1|key2 2013-02-21 18:18 PM|1283483 bytes|value|value 2013-02-21 18:19 PM|1283493 bytes||value ... </code></pre> <p>I have looked at solutions such as this <a href="https://stackoverflow.com/questions/1729824/transpose-a-file-in-bash">An efficient way to transpose a file in Bash</a>, but it seems like I have a different case here. The awk solution works partially for me, it keeps outputting all the rows into a long list of columns, but I need for the columns to be constrained to a unique list.</p> <pre><code>awk -F': ' ' { for (i=1; i&lt;=NF; i++) { a[NR,i] = $i } } NF&gt;p { p = NF } END { for(j=1; j&lt;=p; j++) { str=a[1,j] for(i=2; i&lt;=NR; i++){ str=str" "a[i,j]; } print str } }' filename </code></pre> <p><strong>UPDATE</strong></p> <p>Thanks to all of you who providing your solutions. Some of them look very promising, but I think my version of the tools might be outdated and I am getting some syntax errors. What I am seeing now is that I did not start off with very clear requirements. Kudos to sputnick for being the first one to offer the solution before I spelled out the full requirements. I have had a long day when I wrote the question and thus it was not very clear.</p> <p>My goal is to come up with a very generic solution for parsing multiple lists of items into column format. I am thinking the solution does not need to support more than 255 columns. Column names are not going to be known ahead of time, this way the solution will work for anyone, not just me. The two known things are the separator between kev/value pairs (": ") and a separator between lists (empty line). It would be nice to have a variable for those, so that they are configurable for others to reuse this.</p> <p>From looking at proposed solutions, I realize that a good approach is to do two passes over the input file. First pass is to gather all the column names, optionally sort them, then print the header. Second to grab the values of the columns and print them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload