Re: replace

"Thierry Blanc [email protected] [sed-users]" <[email protected]> Thu, 23 Feb 2017 21:39:09 +0100
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
well, I am not sure if order is important.

But I remark the Freudian title the file with my results ... ;)

sed -r 's|(.*)-(.*)|/\1/{h;s/\1/&/p;g}|' a > temp
this creates a temp file:

    /paul/{h;s/paul/paul-112001/p;g}
    /paul/{h;s/paul/paul-151998/p;g}
    /helen/{h;s/helen/helen-101995/p;g}
    /mike/{h;s/mike/mike-252001/p;g}
    /mike/{h;s/mike/mike-171998/p;g}
    /mike/{h;s/mike/mike-131999/p;g}
    /mike/{h;s/mike/mike-151989/p;g}


sed -rf temp b | sed -r 'N;/(.*)-([0-9]*)\n\1$/{s|\n.*||};P;D'
then use the temp file on file b and remove the unwanted lines.



On 23/02/17 17:32, Tim Chase [email protected] [sed-users] wrote:
> 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
>
>
>
>
>
> ------------------------------------
> Posted by: Tim Chase <[email protected]>
> ------------------------------------
>



[Non-text portions of this message have been removed]