Re: replace

"Tim Chase [email protected] [sed-users]" <[email protected]> Thu, 23 Feb 2017 10:32:32 -0600
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
On 2017-02-23 09:10, Thierry Blanc [email protected] [sed-users]
wrote:
> if you are on bash, you can use something like
> 
> while IFS='-' read -r name number ;
> do
>     sed -rn "/\/$name/s|$name|$name-$number|p" b ;
> done < a
> 
> a and b are file A and file B.

Doesn't quite give the desired output:

$ diff -u thierry_out.txt expected.txt 
--- thierry_out.txt	2017-02-23 10:01:16.487060326 -0600
+++ expected.txt	2017-02-23 09:47:43.667265694 -0600
@@ -1,11 +1,13 @@
 paris/paul-112001
-boston/paul-112001
-london/paul-112001
 paris/paul-151998
+boston/paul-112001
 boston/paul-151998
+london/paul-112001
 london/paul-151998
-san-francisco/helen-101995
 san-francisco/mike-252001
 san-francisco/mike-171998
 san-francisco/mike-131999
 san-francisco/mike-151989
+san-francisco/helen-101995
+san-francisco/dan
+rome/james

So while this is a sed list, I'd reach for awk for the one-off if you
need it to be super-portable to a stripped-down POSIX system, or I'd
use Python if I knew I'd have it available.

$ cat rachid.awk
#!/usr/bin/awk -f
BEGIN {
 FS="[/-]"
}
FILENAME == "a.txt" {
 map[$1][i++] = $2
}
FILENAME != "a.txt" {
 if ($NF in map) {
  for (idx in map[$NF])
   print $0 "-" map[$NF][idx]
 } else {
  print $0
 }
}
$ diff -u <(./rachid.awk a.txt b.txt) expected.txt
$

-tim