1

I have data being displayed on the console in the following format:

 "name1",
 "value1",
 "name2",
 "value2",
 "name3",
 "value3",
 ...
 ...

I need to somehow manipulate this output so that it will be displayed in the following format:

 name1 : value1
 name2 : value2
 name3 : value3
 ...
 ...

I'm not very good with shell scripting but I know my options are sed, tr, or awk (or a combination of the aforementioned). I just can't figure out how to accomplish what it is I need here. Thanks in advance!

2 Answers2

3

Pipe it into:

 | sed 's/^ *"\|", *$//g' | paste -d: - -

sed to remove the quotes, paste to join 2 consecutive lines together with a colon.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Perfect! I was struggling with the 'paste' portion of this problem -- never had to use it before so I wasn't aware that command existed. Thanks @glenn – p0ptha5tack Mar 26 '15 at 23:18
1

glenn jackmans' paste solution is quite elegant but I figured I'd throw in some other solutions.

With sed : sed -ne '1~2{s/$/ : /;N;s/[",]\|\n //g;p}'

For every odd line add : to the end, read the next line into the current pattern space (N), remove any quotes and commas and newline+space combinations and print.

With awk : awk '{gsub(/[",]|^ */,"")} NR%2{printf $0 " : "} !(NR%2)'

Replace all quotes, commas and line-leading sapces. For all odd lines (lines where NR%2 is non-zero) append : and print without a newline. For all even lines (lines where NR%2 is zero) print the line (finishing the previous line).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148