match values in first column of two files and join the matching lines in a new file

前端 未结 2 1299
陌清茗
陌清茗 2021-01-19 04:55

I need to find matches with the string in column 1 ($1) in file1.txt with the string in column 1 ($1) in file2.txt. Then I want to join the lines where there was a match in

相关标签:
2条回答
  • 2021-01-19 05:20
    awk 'BEGIN {
      FS = OFS = "\t"
      }
    NR == FNR {
      # while reading the 1st file
      # store its records in the array f
      f[$1] = $0
      next
      }
    $1 in f {
      # when match is found
      # print all values
      print f[$1], $0
      }' file1 file2 
    
    0 讨论(0)
  • 2021-01-19 05:35

    If you don't mind the output being ordered by the first column then you can use this invocation of the join command:

    join <(sort file1.txt) <(sort file2.txt) >out.txt
    
    0 讨论(0)
提交回复
热议问题