NicheArchitecture has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,

here is a pastebin link to my question in case I formatted this incorrectly: https://pastebin.com/E1sy9s2B

I have a list of text correspondence that references a “handle_id” number for contact info. The “handle_id” is associated with a phone number or email address which is stored in a list in a separate file. I am trying to replace the “handle_id =” string found in messages.txt with the “id =” string found in contacts.txt The lists look like this:

contacts.txt :

handle_id = 868 id = aust@email.com

handle_id = 920 id = tara@email.ca

handle_id = 921 id = gram@email.ca

handle_id = 922 id = +17786771234

handle_id = 923 id = +12508891234

handle_id = 924 id = +12502171234

handle_id = 925 id = +12508891234

handle_id = 926 id = +12505801234

handle_id = 927 id = +17789701234

handle_id = 970 id = +16137941234

messages.txt :

handle_id = 1541 Sent Fri Mar 23 13:41:02 2018

text = Hi Partner: Called your house & learned you are out at a mtg.

handle_id = 1541 Received Fri Mar 23 16:12:25 2018

text = I got you bud

handle_id = 970 Sent Fri Mar 23 16:13:56 2018

text = Please pickup milk.

What I’m trying to do is search for the “handle_id = XXX” string in messages.txt and replace it with the associated “id =” string found in contacts.txt. To put it another way, I want to search messages.txt for any matches from “column A” in contacts.txt, if a match is found, overwrite it with the “id =” string next to the match in “column B” so that it looks something like this:

MESSAGE LIST:

id = +12507741234 Sent Fri Mar 23 13:41:02 2018

text = Hi Partner: Called your house & learned you are out at a mtg.

id = +12507741234 Received Fri Mar 23 16:12:25 2018

text = I got you bud

id = +16137941234 Sent Fri Mar 23 16:13:56 2018

text = Please pickup milk.

Any help would be much appreciated, thanks!

  • Comment on Search and replace from one list to another

Replies are listed 'Best First'.
Re: Search and replace from one list to another
by AnomalousMonk (Archbishop) on May 09, 2018 at 00:39 UTC

    I'm assuming you can process the relevant files for their contents, so the problem is essentially the substitution:

    c:\@Work\Perl\monks>perl -wMstrict -le "my %hid = ( '1541' => '+12507741234', '926' => '+12505801234', '927' => '+17789701234', '970' => '+16137941234', ); ;; my ($rx_hid) = map qr{ (?<! \d) (?: $_) (?! \d) }xms, join '|', map quotemeta, reverse sort keys %hid ; print $rx_hid; ;; my $msg = join qq{\n}, 'handle_id = 1541 Sent Fri Mar 23 13:41:02 2018', 'text = Hi Partner: Called your house & you are at a mtg.', 'handle_id = 1541 Received Fri Mar 23 16:12:25 2018', 'text = I got you bud', 'handle_id = 970 Sent Fri Mar 23 16:13:56 2018', 'text = Please pickup milk.', ''; print qq{[[$msg]]}; ;; $msg =~ s{ (?i) handle_id \s* = \s* ($rx_hid) } {id = $hid{$1}}xmsg; print qq{<<$msg>>}; " (?msx-i: (?<! \d) (?: 970|927|926|1541) (?! \d) ) [[handle_id = 1541 Sent Fri Mar 23 13:41:02 2018 text = Hi Partner: Called your house & you are at a mtg. handle_id = 1541 Received Fri Mar 23 16:12:25 2018 text = I got you bud handle_id = 970 Sent Fri Mar 23 16:13:56 2018 text = Please pickup milk. ]] <<id = +12507741234 Sent Fri Mar 23 13:41:02 2018 text = Hi Partner: Called your house & you are at a mtg. id = +12507741234 Received Fri Mar 23 16:12:25 2018 text = I got you bud id = +16137941234 Sent Fri Mar 23 16:13:56 2018 text = Please pickup milk. >>
    See also haukex's Building Regex Alternations Dynamically.

Re: Search and replace from one list to another
by NetWallah (Canon) on May 09, 2018 at 03:50 UTC
    One Liner (Because it is possible && fun) option:
    perl -MPath::Tiny -pe 'BEGIN{m/_id = (\d+) id = (\S+)/ and $id{$1}=$2 + for path("contacts.txt")->lines} s/handle_id = (\d+)/$id{$1}/' messa +ges.txt

                    Memory fault   --   brain fried