Gabriele
Gabriele

Reputation: 35

Replace words in a list with words in another list that share some letters

I have a very long list of words (with duplications) like this:

list1.txt

Apple
Pear
Banana
Banana
Banana
Banana
Orange
Orange

I want to replace every words in the list1.txt with another list of words (with no duplications), list2.txt, that contains the same words of the list1.txt plus something more:

list2.txt

Apple_red
Pear_green
Banana_yellow
Orange_orange

I need an output like this:

list3.txt

Apple_red
Pear_green
Banana_yellow
Banana_yellow
Banana_yellow
Banana_yellow
Orange_orange
Orange_orange

Any suggestions? Thank you!

Upvotes: 0

Views: 56

Answers (2)

anubhava
anubhava

Reputation: 785246

You may use this awk:

awk -F'_' 'NR==FNR{a[$1]=$0; next} {print a[$1]}' list2.txt list1.txt

Apple_red
Pear_green
Banana_yellow
Banana_yellow
Banana_yellow
Banana_yellow
Orange_orange
Orange_orange

Upvotes: 1

potong
potong

Reputation: 58430

This might work for you:

uniq list1.txt | paste -d/ - list2.txt | sed 's#.*#s/&/#' | sed -f - list1.txt
  • Remove duplicates from list1.txt
  • Join list1.txt and list2.txt using a / as separator.
  • Create a sed substitution script from result of above.
  • Apply the sed script to list1.txt original file

Upvotes: 0

Related Questions