I have two text files in the following format:
The first is this on every line:
Key1:Value1
The second is this:
Key2:Value2
Is there a way I can replace Value1 in file1 by the Value2 obtained from using it as a key in file2?
For example:
file1:
foo:hello
bar:world
file2:
hello:adam
bar:eve
I would like to get:
foo:adam
bar:eve
There isn’t necessarily a match between the two files on every line. Can this be done neatly in awk or something, or should I do it naively in Python?
Create two dictionaries, one for each file. For example:
Or if you prefer a one-liner:
Then you could do something like:
Another way is you could generate the key-value pairs in reverse for file1 and use sets. For example, if your file1 contains
foo:bar, your file1 dict is{bar: foo}.Basically, you can quickly find common elements using set intersection, so those elements are guaranteed to be in file2 and you don’t waste time checking for their existence.
Edit: As pointed out by @pepr You can use
collections.OrderedDictfor the first method if order is important to you.