Find and replace multiple strings in a list with equivalent values in another file [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have the following file which called list.txt. This file contain the following:



AA --> TX1
BB --> TX2
CC --> TX3


Also, I have the following file which called A.txt. which contain the following text



AA XXXXXXXXXXXXXXXXXXXXXXXX BB
XXXXXXXX CC


I would like to replace AA, BB, and CC with its equivalent values from the file list.txt, so the output is as follows:



TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3


How can I do that?










share|improve this question















closed as unclear what you're asking by Rui F Ribeiro, steve, Sparhawk, Thomas, don_crissti Sep 8 at 16:45


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Are the --> exist in the list file as an actual column?
    – Goro
    Sep 8 at 10:31










  • Related: unix.stackexchange.com/q/334067/117549 and unix.stackexchange.com/q/57731/117549
    – Jeff Schaller
    Sep 8 at 11:55










  • Possible duplicate of String replacement using a dictionary
    – don_crissti
    Sep 8 at 16:45














up vote
0
down vote

favorite












I have the following file which called list.txt. This file contain the following:



AA --> TX1
BB --> TX2
CC --> TX3


Also, I have the following file which called A.txt. which contain the following text



AA XXXXXXXXXXXXXXXXXXXXXXXX BB
XXXXXXXX CC


I would like to replace AA, BB, and CC with its equivalent values from the file list.txt, so the output is as follows:



TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3


How can I do that?










share|improve this question















closed as unclear what you're asking by Rui F Ribeiro, steve, Sparhawk, Thomas, don_crissti Sep 8 at 16:45


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Are the --> exist in the list file as an actual column?
    – Goro
    Sep 8 at 10:31










  • Related: unix.stackexchange.com/q/334067/117549 and unix.stackexchange.com/q/57731/117549
    – Jeff Schaller
    Sep 8 at 11:55










  • Possible duplicate of String replacement using a dictionary
    – don_crissti
    Sep 8 at 16:45












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the following file which called list.txt. This file contain the following:



AA --> TX1
BB --> TX2
CC --> TX3


Also, I have the following file which called A.txt. which contain the following text



AA XXXXXXXXXXXXXXXXXXXXXXXX BB
XXXXXXXX CC


I would like to replace AA, BB, and CC with its equivalent values from the file list.txt, so the output is as follows:



TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3


How can I do that?










share|improve this question















I have the following file which called list.txt. This file contain the following:



AA --> TX1
BB --> TX2
CC --> TX3


Also, I have the following file which called A.txt. which contain the following text



AA XXXXXXXXXXXXXXXXXXXXXXXX BB
XXXXXXXX CC


I would like to replace AA, BB, and CC with its equivalent values from the file list.txt, so the output is as follows:



TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3


How can I do that?







text-processing find replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 8 at 12:54









Goro

5,22552459




5,22552459










asked Sep 8 at 8:39









Worapong Janloy

113




113




closed as unclear what you're asking by Rui F Ribeiro, steve, Sparhawk, Thomas, don_crissti Sep 8 at 16:45


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Rui F Ribeiro, steve, Sparhawk, Thomas, don_crissti Sep 8 at 16:45


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • Are the --> exist in the list file as an actual column?
    – Goro
    Sep 8 at 10:31










  • Related: unix.stackexchange.com/q/334067/117549 and unix.stackexchange.com/q/57731/117549
    – Jeff Schaller
    Sep 8 at 11:55










  • Possible duplicate of String replacement using a dictionary
    – don_crissti
    Sep 8 at 16:45
















  • Are the --> exist in the list file as an actual column?
    – Goro
    Sep 8 at 10:31










  • Related: unix.stackexchange.com/q/334067/117549 and unix.stackexchange.com/q/57731/117549
    – Jeff Schaller
    Sep 8 at 11:55










  • Possible duplicate of String replacement using a dictionary
    – don_crissti
    Sep 8 at 16:45















Are the --> exist in the list file as an actual column?
– Goro
Sep 8 at 10:31




Are the --> exist in the list file as an actual column?
– Goro
Sep 8 at 10:31












Related: unix.stackexchange.com/q/334067/117549 and unix.stackexchange.com/q/57731/117549
– Jeff Schaller
Sep 8 at 11:55




Related: unix.stackexchange.com/q/334067/117549 and unix.stackexchange.com/q/57731/117549
– Jeff Schaller
Sep 8 at 11:55












Possible duplicate of String replacement using a dictionary
– don_crissti
Sep 8 at 16:45




Possible duplicate of String replacement using a dictionary
– don_crissti
Sep 8 at 16:45










1 Answer
1






active

oldest

votes

















up vote
2
down vote













You can do this:



#!/bin/bash
for i in AA BB CC;do
var=$(grep $i list.txt | awk 'print $2')

while read $i ; do
sed -i -e 's/'$i'/'$var'/g' A.txt;
done <A.txt
done


The output is as follows:



$cat A.txt
TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3





share|improve this answer






















  • Thk you for fast response In my list I would like to replace any text not only AA and I have multiple value in the list.
    – Worapong Janloy
    Sep 8 at 9:09










  • The response above assumes that --> is not exist in the list file. If it is an actual column in the list then you have to replace awk 'print $2' byawk 'print $3'.
    – Goro
    Sep 8 at 10:27







  • 1




    This is very brittle, it's will fall if the strings contain bash, grep or sed special characters. The first is easily fixed with quoting, the later two not so easily
    – user000001
    Sep 8 at 10:40










  • Oh Thank you so much I will test the script.
    – Worapong Janloy
    Sep 8 at 11:17










  • It don't work If I have special character for example IF_AAA-10.10.10.32-Host1.
    – Worapong Janloy
    Sep 8 at 16:21

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













You can do this:



#!/bin/bash
for i in AA BB CC;do
var=$(grep $i list.txt | awk 'print $2')

while read $i ; do
sed -i -e 's/'$i'/'$var'/g' A.txt;
done <A.txt
done


The output is as follows:



$cat A.txt
TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3





share|improve this answer






















  • Thk you for fast response In my list I would like to replace any text not only AA and I have multiple value in the list.
    – Worapong Janloy
    Sep 8 at 9:09










  • The response above assumes that --> is not exist in the list file. If it is an actual column in the list then you have to replace awk 'print $2' byawk 'print $3'.
    – Goro
    Sep 8 at 10:27







  • 1




    This is very brittle, it's will fall if the strings contain bash, grep or sed special characters. The first is easily fixed with quoting, the later two not so easily
    – user000001
    Sep 8 at 10:40










  • Oh Thank you so much I will test the script.
    – Worapong Janloy
    Sep 8 at 11:17










  • It don't work If I have special character for example IF_AAA-10.10.10.32-Host1.
    – Worapong Janloy
    Sep 8 at 16:21














up vote
2
down vote













You can do this:



#!/bin/bash
for i in AA BB CC;do
var=$(grep $i list.txt | awk 'print $2')

while read $i ; do
sed -i -e 's/'$i'/'$var'/g' A.txt;
done <A.txt
done


The output is as follows:



$cat A.txt
TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3





share|improve this answer






















  • Thk you for fast response In my list I would like to replace any text not only AA and I have multiple value in the list.
    – Worapong Janloy
    Sep 8 at 9:09










  • The response above assumes that --> is not exist in the list file. If it is an actual column in the list then you have to replace awk 'print $2' byawk 'print $3'.
    – Goro
    Sep 8 at 10:27







  • 1




    This is very brittle, it's will fall if the strings contain bash, grep or sed special characters. The first is easily fixed with quoting, the later two not so easily
    – user000001
    Sep 8 at 10:40










  • Oh Thank you so much I will test the script.
    – Worapong Janloy
    Sep 8 at 11:17










  • It don't work If I have special character for example IF_AAA-10.10.10.32-Host1.
    – Worapong Janloy
    Sep 8 at 16:21












up vote
2
down vote










up vote
2
down vote









You can do this:



#!/bin/bash
for i in AA BB CC;do
var=$(grep $i list.txt | awk 'print $2')

while read $i ; do
sed -i -e 's/'$i'/'$var'/g' A.txt;
done <A.txt
done


The output is as follows:



$cat A.txt
TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3





share|improve this answer














You can do this:



#!/bin/bash
for i in AA BB CC;do
var=$(grep $i list.txt | awk 'print $2')

while read $i ; do
sed -i -e 's/'$i'/'$var'/g' A.txt;
done <A.txt
done


The output is as follows:



$cat A.txt
TX1 XXXXXXXXXXXXXXXXXXXXXXXX TX2
XXXXXXXX TX3






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 8 at 11:44

























answered Sep 8 at 8:49









Goro

5,22552459




5,22552459











  • Thk you for fast response In my list I would like to replace any text not only AA and I have multiple value in the list.
    – Worapong Janloy
    Sep 8 at 9:09










  • The response above assumes that --> is not exist in the list file. If it is an actual column in the list then you have to replace awk 'print $2' byawk 'print $3'.
    – Goro
    Sep 8 at 10:27







  • 1




    This is very brittle, it's will fall if the strings contain bash, grep or sed special characters. The first is easily fixed with quoting, the later two not so easily
    – user000001
    Sep 8 at 10:40










  • Oh Thank you so much I will test the script.
    – Worapong Janloy
    Sep 8 at 11:17










  • It don't work If I have special character for example IF_AAA-10.10.10.32-Host1.
    – Worapong Janloy
    Sep 8 at 16:21
















  • Thk you for fast response In my list I would like to replace any text not only AA and I have multiple value in the list.
    – Worapong Janloy
    Sep 8 at 9:09










  • The response above assumes that --> is not exist in the list file. If it is an actual column in the list then you have to replace awk 'print $2' byawk 'print $3'.
    – Goro
    Sep 8 at 10:27







  • 1




    This is very brittle, it's will fall if the strings contain bash, grep or sed special characters. The first is easily fixed with quoting, the later two not so easily
    – user000001
    Sep 8 at 10:40










  • Oh Thank you so much I will test the script.
    – Worapong Janloy
    Sep 8 at 11:17










  • It don't work If I have special character for example IF_AAA-10.10.10.32-Host1.
    – Worapong Janloy
    Sep 8 at 16:21















Thk you for fast response In my list I would like to replace any text not only AA and I have multiple value in the list.
– Worapong Janloy
Sep 8 at 9:09




Thk you for fast response In my list I would like to replace any text not only AA and I have multiple value in the list.
– Worapong Janloy
Sep 8 at 9:09












The response above assumes that --> is not exist in the list file. If it is an actual column in the list then you have to replace awk 'print $2' byawk 'print $3'.
– Goro
Sep 8 at 10:27





The response above assumes that --> is not exist in the list file. If it is an actual column in the list then you have to replace awk 'print $2' byawk 'print $3'.
– Goro
Sep 8 at 10:27





1




1




This is very brittle, it's will fall if the strings contain bash, grep or sed special characters. The first is easily fixed with quoting, the later two not so easily
– user000001
Sep 8 at 10:40




This is very brittle, it's will fall if the strings contain bash, grep or sed special characters. The first is easily fixed with quoting, the later two not so easily
– user000001
Sep 8 at 10:40












Oh Thank you so much I will test the script.
– Worapong Janloy
Sep 8 at 11:17




Oh Thank you so much I will test the script.
– Worapong Janloy
Sep 8 at 11:17












It don't work If I have special character for example IF_AAA-10.10.10.32-Host1.
– Worapong Janloy
Sep 8 at 16:21




It don't work If I have special character for example IF_AAA-10.10.10.32-Host1.
– Worapong Janloy
Sep 8 at 16:21


Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?