Find and replace text in one file using text from another

Hi, how could I do the next:

I have in one file, let’s call it file A, some text. it has lines such as:

lorem ipsum
name = ABCD0
dolor sit amet
cataliniria
name = ADEF1
some text
more text
name = DEFX2

each name is unique

What I need is find and replace the text after name = with the second column from a different source file, let’s call this file B.

Rows in file B are:

ABCD0, firstReplacedText
ADEF1, secondReplacement
DEFX2, thirdTextToReplace

So, after all the finding and replacing, file A will end up looking like this:

lorem ipsum
name = firstReplacedText
dolor sit amet
cataliniria
name = secondReplacement
some text
more text
name = thirdTextToReplace

Any ideas or pointers about how to tackle this?
thanks

—-
Edit:
Will try this:

The easiest way would be to read the first file, and then read the second file line by line, and perform a search & replace for each one. Depending on the size of the files and the number fo replacements, that might be slow, but probably it would be fast enough.

If the file is large (at least MBs) and the number of replacements is large (at least hundreds, probably thousands), then you might get to the point where that method is not fast enough.

In that case, read the second file in to a dictionary, and then do a single search and replace, something like:

image

1 Like

Thanks!
The file has about 100k rows, but since the replacement is a one-time thing, will leave it running until it finishes.

I know you said this is a one-time thing, but it intrigued me. If it were me, I'd write some JXA to do it. But...

I'm pretty sure you can accomplish this using the BASH command "sed". Check out this: https://askubuntu.com/questions/972074/how-to-find-and-replace-multiple-strings-in-multiple-html-files.

It would take some effort to learn exactly how to do this, but I suspect if you figure it out, you'll have learned a tool (sed) that will be invaluable. I've never used sed, but I worked with a guy who could do almost anything using it (and some other commands like it).

Like I said, I know this was a one-off, but I thought I'd mention it.

1 Like

How do I read a file into a dictionary? time to read the Wiki...

sed, awk, someday, someday :slight_smile:


Edit: Found this script by @Tom , did a quick test and seems to work exactly as needed.

Edit 2:
Also just added this plugin to read a file into a JSON Dictionary plugin, and avoid bogging KM down:

maybe spoke too soon on the one-time thing :slight_smile:

I totally hear you on that. Been planning to learn that stuff for 25 years. :smile:

Those topics look interesting. By the way, how did you link them into your post, so it shows the summary like that? I forgot how to do that.

1 Like

just paste the complete URL and it generates the summary automatically

ie http://url

1 Like

As someone who learnt sed and awk a couple decades ago, I will suggest instead you just learn perl. sed and awk have very specific and peculiar and unique syntaxes and are quite limited in what they can do, whereas perl has a very specific and peculiar and unique syntax, but can easily do everything either of them can do and anything else text-parsing wise. Or anything-wise really. perl is my go-to language for scripting, and while it is old and clearly the developers fell off the rails a long long time ago, and python is a lot more popular these days, your time spend learning perl would be much better spent than time spent learning sed or awk. IMHO.

In any event, Keyboard Maestro can do this pretty trivially, and I expect it would be fast enough even with 100k rows. The slow part is setting up the dictionary. In a quick test I ran, Keyboard Maestro can process about 250 lines per second creating the dictionary, so 400 seconds to set up the dictionary. If the dictionary is fixed, then this can be done once and never needs to be done again until the dictionary changes. The actual search and replace is pretty quick, depending on the size of the tile and the number fo changes.

image

5 Likes

Thanks for that (perl), Peter. I will definitely look into it. I love having more tools, regardless of whether they're software tools, or woodworking tools. :grinning_face_with_smiling_eyes:

1 Like