Find and replace multiple strings in a list with equivalent values in another file [closed]
Clash 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?
text-processing find replace
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.
add a comment |Â
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?
text-processing find replace
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
add a comment |Â
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?
text-processing find replace
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
text-processing find replace
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
add a comment |Â
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
add a comment |Â
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
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 replaceawk '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
 |Â
show 1 more comment
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
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 replaceawk '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
 |Â
show 1 more comment
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
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 replaceawk '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
 |Â
show 1 more comment
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
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
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 replaceawk '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
 |Â
show 1 more comment
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 replaceawk '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
 |Â
show 1 more comment
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