Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 3503  / 1 Year ago, sun, november 20, 2022, 12:11:12

let us say i have two files a.txt, b.txt



a.txt

87621 3bde NDF
87621 2dfg NDF
87621 cdef NDF
87621 abcd NDF

b.txt

93291 abcd NDF
93291 2dfg NDF
93291 adbf NDF
93291 gdrg NDF


My script should create a file , by matching column 2 of both the files
and create a file with matching string in new file c.txt like below



c.txt

2dfg
abcd


I tried several options, but not succeded, can you help !


More From » bash

 Answers
6
comm -12 <(awk '{print $2}' a.txt | sort) <(awk '{print $2}' b.txt | sort) > c.txt


Explanation




  • <(...) is a process substitution. i.e. the output of the commands within the (...) is substituted and used as the two inputs to comm.

  • awk '{print $2}' a.txt prints only the second field of each line.

  • sort sorts the output ready for input into comm.

  • comm compares two sorted files. The -12 flags suppress the lines that are unique to each file, only printing the common lines (see man comm for more details).


[#26379] Sunday, November 20, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lawain

Total Points: 150
Total Questions: 106
Total Answers: 101

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;