How can I create a table from key-value pairs?

-1

I want to check the differences between lots of key value pairs. I know I could write some code to do this, but I feel there is going to be an easier way to accomplish this. These values come from HTTP request URLs.

Say I have the following key value pairs, they are not always in order:

Set 1:

a=1
b=2
c=3

Set 2:

a=2
c=5
b=7

Set 3:

b=5
c=2
a=8

Set 4:

a=1
c=6
b=7

I'm looking for an output like this, just so I can easily see differences:

The output I'm looking for

How can I achieve this easily, without having to write my own program?

mt025

Posted 2017-02-15T20:56:00.137

Reputation: 2 392

This question is off topic here, asking for product / service / learning material recommendation. Please read what's on-topic here

– Máté Juhász – 2017-02-15T21:01:06.623

Answers

1

You could use awk for this:

cat file1 file2 | awk -F "=" '{i[$1]=(i[$1] " " $2)} END {for (key in i) { print key " " i[key]}}'

Example output:

d  104 42
a  8 9
b  9 13
c  22 -1

Additionally pipe to sort if you want the values in a consistent order.

Devin R

Posted 2017-02-15T20:56:00.137

Reputation: 196

Perfect, thanks! I never even thought to use *nix! – mt025 – 2017-02-15T21:09:05.577

Please don't answer off-topic questions. – DavidPostill – 2017-02-15T21:14:09.687

1I didn't think it was too off-topic when I wrote my answer. I just assumed that by "application" he meant "some combination of Linux tools". Apparently he actually did mean "application"! – Devin R – 2017-02-15T21:15:39.230

2Agreed, the user just had a problem they wanted to solve. It's easy enough to fix the question to “how can I do …?” cc @DavidPostill – slhck – 2017-02-15T21:36:44.057

@slhck Fair point. CV removed. ;p – DavidPostill – 2017-02-15T21:52:39.337