Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash. Get intersection from multiple files

So let me explain this a bit more:

I have a directory called tags that has a file for each tag, something like:

tags/
    t1
    t2
    t3

In each of the tag files is a structure like:

<inode> <filename> <filepath>

Of course, each tag file will have a list of many files with that tag (but a file can only appear in the one tag file once). And a file may be in multiple tag files.

What I want to be able to do is call a command like

tags <t1> <t2> 

and have it list the files that have BOTH the tags t1 and t2 in a nice way.

My plan right now was to make a temp file. Basically output the entire file of t1 into it. Then run through each line in t2 and do an awk on the file. And just keep doing that.

But I am wondering if anyone has any other ways. I am not overly familiar with awk, grep etc.

like image 556
Jonovono Avatar asked Oct 06 '13 21:10

Jonovono


1 Answers

Can you use

sort t1 t2 | uniq -d

This will combine the two files, sort them, and then display only the lines that appear more than once: that is, the ones that appear in both files.

This assumes that each file contains no duplicates within it, and that the inodes are the same in all the structures for a particular file.

like image 172
Adam Liss Avatar answered Oct 13 '22 06:10

Adam Liss