0

input file:

col01 20083d70-1bf0-40f4-9df9-6247d587c085 text text
col02 d233d76d-e60d-442e-9b44-f0e1dc9ebefb text text
col03 20083d70-1bf0-40f4-9df9-6247d587c085 text text
col04 20083d70-1bf0-40f4-9df9-6247d587c085 text text
col05 b26063fa-93e4-414d-82f2-7c2924c81cc6 text text
col06 dc3e2f9b-05bf-49b4-a63e-c4bc09524eb3 text text
col07 d9f5cf1d-a8f9-488d-b088-b7ad00b67ec6 text text
col08 b26063fa-93e4-414d-82f2-7c2924c81cc6 text text
col09 d9a558ff-494a-4ada-8ec2-e9b2f9178599 text text
col10 b26063fa-93e4-414d-82f2-7c2924c81cc6 text text
col11 b26063fa-93e4-414d-82f2-7c2924c81cc6 text text
col12 4d78f804-18f6-4cc8-98a3-c4aedc60aebf text text
col13 0fe487fe-2ff2-4ca4-adf8-de3c19115dde text text
col14 581207a4-f4a5-4373-806b-0e1f7c298bb1 text text

reference file:

0fe487fe-2ff2-4ca4-adf8-de3c19115dde group1
20083d70-1bf0-40f4-9df9-6247d587c085 group2
4d78f804-18f6-4cc8-98a3-c4aedc60aebf group3
581207a4-f4a5-4373-806b-0e1f7c298bb1 group4
b26063fa-93e4-414d-82f2-7c2924c81cc6 group5
d233d76d-e60d-442e-9b44-f0e1dc9ebefb group6
d9a558ff-494a-4ada-8ec2-e9b2f9178599 group7
d9f5cf1d-a8f9-488d-b088-b7ad00b67ec6 group8
dc3e2f9b-05bf-49b4-a63e-c4bc09524eb3 group9

expected output:

col01 20083d70-1bf0-40f4-9df9-6247d587c085(group2) text text
col02 d233d76d-e60d-442e-9b44-f0e1dc9ebefb(group6) text text
col03 20083d70-1bf0-40f4-9df9-6247d587c085(group2) text text
col04 20083d70-1bf0-40f4-9df9-6247d587c085(group2) text text
col05 b26063fa-93e4-414d-82f2-7c2924c81cc6(group5) text text
col06 dc3e2f9b-05bf-49b4-a63e-c4bc09524eb3(group9) text text
col07 d9f5cf1d-a8f9-488d-b088-b7ad00b67ec6(group8) text text
col08 b26063fa-93e4-414d-82f2-7c2924c81cc6(group5) text text
col09 d9a558ff-494a-4ada-8ec2-e9b2f9178599(group7) text text
col10 b26063fa-93e4-414d-82f2-7c2924c81cc6(group5) text text
col11 b26063fa-93e4-414d-82f2-7c2924c81cc6(group5) text text
col12 4d78f804-18f6-4cc8-98a3-c4aedc60aebf(group3) text text
col13 0fe487fe-2ff2-4ca4-adf8-de3c19115dde(group1) text text
col14 581207a4-f4a5-4373-806b-0e1f7c298bb1(group4) text text

right now I can achieve this with sed -i in while loop:

$ cat reference|while read a b;do sed -i s/"$a"/"$a($b)"/ input;done

Could it be done in an easier way, using just the awk and without the while loop, using input and reference file in single awk command processing changing the values only in second column?

1 Answer 1

1

The following approach is purely awk-based and avoids the external loop:

awk 'FNR==NR {key[NR]=$1; ref[NR]=$2; n_refs++; next} {for (i=1;i<=n_refs;i++) if ($2==key[i]) $2=key[i]"("ref[i]")"; print}' reference input

This will process first the "reference" file and generate a list of UUID-"Group nr"-pairs (actually two arrays with corresponding entries) from that file.

Once this file is processed (indicated by FNR, the per-file record number, differing from NR, the global record number), and the "main input file" is being processed, it will loop over all associations found and add the group ID corresponding to the UUID in second column by simply comparing the current content of the second column to all registered UUIDs.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.