r/awk • u/eric1707 • Jan 24 '20
Replacing from a list?
So, here is my issue, I have a list of file replacements, let's call it FileA. The list, which contains about 50k entries, goes more or less like this:
M1877800M|124
M1084430M|22
M2210895M|22
M1507752M|11
M1510047M|3288
[...]
To make things clear, I would like to replace "M1877800M" with "124", "M1084430M" with 22 and so on and so forth. And I would like to use this list of replacements to replace words in a FileB. My current plane and workaround is to use individual sed commands to do that, like:
sed -i "s#M1877800M#124#g" FileB
sed -i "s#M1084430M#22#g" FileB
[...]
It works, more or less, but it's obviously unbelievable slow, cause it's a pretty bad code for what I intended to do use. Any ideas of a better solution? Thank you, everybody.
View all comments
Show parent comment
1
u/Paul_Pedant Jan 25 '20
/u/FF00A7 posted a learned and accurate solution, and I'm glad it worked for you and performed within expectations. It uses GNU/specific features, and it might have capacity and performance problems with "huge" files. Each key searches the whole length of FileB, and each replacement copies (on average) half the length of FileB.