I would like to transfer the list of elements (key / value pairs) to the table format. The solution could be a bash script, awk, sed, or some other way.
Suppose I have a long list, for example:
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 ...
I would like to transfer to a table format with a tab or other delimiter to look like this:
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 ...
or like this:
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 ...
I looked at solutions such as Efficient way to transfer a file to Bash , but it looks like I have another case. The awk solution works partially for me, it outputs all the rows in a long list of columns, but I need the columns to be bound to a unique list.
awk -F': ' ' { for (i=1; i<=NF; i++) { a[NR,i] = $i } } NF>p { p = NF } END { for(j=1; j<=p; j++) { str=a[1,j] for(i=2; i<=NR; i++){ str=str" "a[i,j]; } print str } }' filename
UPDATE
Thanks to everyone who provides your solutions. Some of them look very promising, but I think my version of the tools may be outdated and I get some syntax errors. Now I see that I did not start with very clear requirements. Kudos to sputnick for being the first to offer a solution before I set out all the requirements. I had a long day when I wrote a question, and therefore it was not very clear.
My goal is to develop a very general solution for parsing multiple lists of items in column format. I think the solution does not need to support more than 255 columns. Column names will not be known in advance, so the solution will work for everyone, not just for me. Two well-known things are the separator between kev / value (":") pairs and the separator between lists (empty line). It would be nice to have a variable so that they are set up for others to reuse it.
From a look at the proposed solutions, I understand that a good approach is to make two passes over the input file. The first pass is to collect all the column names, sort them if necessary, and then print the header. Secondly, to capture the values โโof the columns and print them.