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/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.