Q:

De-duplicate a key-value text file by key

I have a text file with key-value properties, e.g.:
foo: aaa
bla: hello
bar: bbb
foo: ccc
bar: ddd

I want to deduplicate the file, only keep the last appearance of a key, in this example the result would be:
bla: hello
foo: ccc
bar: ddd

A:

Comments in script:
# recreate input
cat <<EOF >input_file.txt
foo: aaa
bla: hello
bar: bbb
foo: ccc
bar: ddd
EOF

# add line number on front
nl -w1 -s: input_file.txt |
# tac the script to keep the latest key
tac |
# sort unique on keys
sort -t: -k2,2 -u |
# sort on line numbers, to restore original numeration
sort -t: -k1,1 -n |
# remove line numbers
cut -d: -f2-

it will output:
bla: hello
foo: ccc
bar: ddd

If however you don't need original order, you can just:
tac input_file.txt | sort -t: -u -k1,1

that will output:
bar: ddd
bla: hello
foo: ccc

