Two-word processing

I have two text files in the following format:

Firstly, this is on each line:

Key1:Value1

The second is the following:

Key2:Value2

Is there a way to replace Value1in file1with Value2, obtained from using it as a key in file2?

For instance:

file1:

foo:hello
bar:world

file2:

hello:adam
bar:eve

I would like to get:

foo:adam
bar:eve

A match between two files on each line is optional. Can this be done neatly in awk or something like that, or should I do it naively in Python?

+5
source share
7 answers

Create two dictionaries, one for each file. For instance:

file1 = {}
for line in open('file1', 'r'):
    k, v = line.strip().split(':')
    file1[k] = v

Or, if you prefer one liner:

file1 = dict(l.strip().split(':') for l in open('file1', 'r'))

Then you can do something like:

result = {}
for key, value in file1.iteritems():
    if value in file2:
        result[key] = file2[value]

- - 1 . , 1 foo:bar, 1 dict {bar: foo}.

for key in set(file1) & set(file2):
    result[file1[key]] = file2[key]

, , set intersection, 2, .

: @pepr. collections.OrderedDict , .

+3

awk:

awk '
  BEGIN {FS = OFS = ":"}
  NR==FNR {val[$1] = $2; next}
  $1 in val {$2 = val[$1]}
  {print}
}' file2 file1
+2
join -t : -1 2 -2 1 -o 0 2.2 -a 2 <(sort -k 2 -t : file1) <(sort file2)

, .

:

  • -t : -
  • -1 2 - 2 1
  • -2 1 - 1 2
  • -o 0 2.2 - , 2 2 ( )
  • -a 2 - 2
+1

:

file1 = {'foo':'hello', 'bar':'world'}
file2 = {'hello':'adam', 'bar':'eve'}

:

print dict([(i,file2[i]) if i in file2 else (i,file2[j]) if j in file2 else (i,j) for i,j in file1.items()])
{'foo': 'adam', 'bar': 'eve'}

As in your example, you use both keys, and valuesof file1as keysin file2.

0
source

If you are not considering using basic Unix / Linux commands, then this is a solution using paste and awk.

paste file1.txt file2.txt | awk -F ":" '{ print $1":"$3 }'

0
source

This might work for you (maybe GNU sed):

sed 's#\([^:]*\):\(.*\)#/\\(^\1:\\|:\1$\\)/s/:.*/:\2/#' file2 | sed -f - file1
0
source

TXR:

@(next "file2")
@(collect)
@key:@value1
@  (cases)
@    (next "file1")
@    (skip)
@value2:@key
@  (or)
@    (bind value2 key)
@  (end)
@  (output)
@value2:@value1
@  (end)
@(end)

Run:

$ txr subst.txr
foo:adam
bar:eve
0
source

All Articles