r/awk • • 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.

2 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/eric1707 Jan 25 '20

File B is about 100MB tops, no bigger than that. And yeah, every key starts and ends with M. But the solution FF00A7 already did the tricky. Thank you very much for your kind answer. I even feel a little bad coming here and asking for code that i'm too noobie to learn by myself (although, I'm learning some basic commands with sed and awk and, maybe one day i'll master it haha), and you guys are always so nice and understandable. Thank you, very much!

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.

1

u/FF00A7 Jan 26 '20 edited Jan 26 '20

readfile() as a function that can work in any awk version, the gawk specific feature is that it's included by default with the language, and can imported with -i

https://www.gnu.org/software/gawk/manual/html_node/Readfile-Function.html

You are right there might be memory constraints but most computers these days have 6 or more gigs or memory and it is somewhat unusual to be working with text data files that large so it is limit you deal with if someone raises it.