First, if there is a better title for this question, I am all ears!
Is there a better way of doing this?
I have a data file input.txt in this format:
field1=value1
field2=value2
field3=value3
.
.
.
field1=value4
field2=value5
field3=value6
and so on … and would like to end up with:
field1,value1 value4 ... valueM
field2,value2 value5 ... valueN
field3,value3 value6 ... valueO
What I’ve tried:
-
Generate unique keys:
cat input.txt |awk -F"=" '{print $2}' |sort -u > data_key -
Loop over rows in
data_key
#!/bin/bash file=input.txt keys=`cat data_key` for value in $keys do output=`cat $file |grep $value |awk -F"=" '{print $2}' |tr -s '[:space:]' '[ *]' ` echo $value, $output done
The below awk-based solution should work. This uses associative arrays keyed by field names. Values are concatenated as they are encountered.