Using linux commandline, i need to output the lines from text file2 that are already found in file1.
File1:
C A G E B D H F
File2:
N I H J K M D L A
Output:
A D H
Thanks!
Advertisement
Answer
A more flexible tool to use would be awk
awk 'NR==FNR{lines[$0]++; next} $1 in lines'
Example
$ awk 'NR==FNR{lines[$0]++; next} $1 in lines' file1 file2
H
D
A
What it does?
NR==FNR{lines[$0]++; next}NR==FNRchecks if the file number of records is equal to the overall number of records. This is true only for the first file,file1lines[$0]++Here we create an associative array with the line,$0in file 1 as index.
$0 in linesThis line works only for the second file because of thenextin previous action. This checks if the line in file 2 is there in the saved arraylines, if yes the default action of printing the entire line is taken
Awk is more flexible than the grep as you can columns in file1 with any column in file 2 and decides to print any column rather than printing the entire line