Re: replace

"[email protected] [sed-users]" <[email protected]> 05 Mar 2017 21:34:37 +0000
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
I would have preferred the OP to at least make a stab at it. But I might as well give the solution that I came up with weeks ago. Especially because the OP at least explained the problem clearly and provided good input and output test cases. 
 

 The solution has two loops. The outer loop sequentially reads and parses lines from B.txt file. For each line read, the inner loop sequentially reads and parses lines from A.txt file. If the names (eg, paul) match, output in the required format is printed.
 

 Daniel
 

 $ cat A.txt
paul-112001
paul-151998
helen-101995
mike-252001
mike-171998
mike-131999
mike-151989
 

 $ cat B.txt
paris/paul
boston/paul
london/paul
san-francisco/mike
san-francisco/helen
san-francisco/dan
rome/james

 

 $ cat rachid.sh
while read line_b; do
  city_b=${line_b%%/*} # cut off end
  name_b=${line_b##*/} # cut off start
  found_match=n
  while read line_a; do
    name_a=${line_a%%-*} # cut off end
    code_a=${line_a##*-} # cut off start
    if [ $name_a = $name_b ]; then
      echo $city_b/$name_b-$code_a
      found_match=y
    fi
  done < A.txt
  if [ $found_match = n ]; then
    echo $city_b/$name_b
  fi
done < B.txt
 

 $ ./rachid.sh
paris/paul-112001
paris/paul-151998
boston/paul-112001
boston/paul-151998
london/paul-112001
london/paul-151998
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

 

 

 



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