title: How to do the below task in shell script using grep but without a for loop?
body: <p>I have a file, say A.txt, which has roughly around 80,000 lines but 2 fields. Example lines look like below:</p>
  
  <pre><code>utt1 f 
  utt2 f 
  utt3 m 
  utt4 f
  </code></pre>
  
  <p>I have a second file B.txt, which has more way more lines than A.txt and  has two fields. Example lines look like below</p>
  
  <pre><code>utt1_1 somethingHere    
  utt1_2 somethingElse    
  utt2_1 someText    
  utt2_2 somemore    
  utt2_3 someMore    
  utt3_1 someText    
  utt4_1 somemore    
  utt4_2 abcd    
  utt4_3 aanan
  </code></pre>
  
  <p>The first field in B.txt is basically the same as first field in A.txt followed by _something (Each line is repeated variable number of times).</p>
  
  <p>My task is to make a file which looks like this</p>
  
  <pre><code>utt1_1 f    
  utt1_2 f    
  utt2_1 f    
  utt2_2 f    
  utt2_3 f    
  utt3_1 m    
  utt4_1 f    
  utt4_2 f    
  utt4_3 f
  </code></pre>
  
  <p>'f' and 'm' starts for gender. in other words what I want to do is get utt and gender information from A.txt and grep for the utterance in B.txt and map the first field with the right gender. I am doing it using a while loop as follows:</p>
  
  <pre><code>while read utt gen; do
     grep $utt B.txt | awk -v gen=$gen '{print $1" "gen}'
  done &lt; A.txt
  </code></pre>
  
  <p>Since, I am doing it in a loop and length of A.txt is big it is taking a lot of time to complete the task. Could you suggest me a faster way to do it using bash?</p>
  
  <p>Thank you</p>