msohail
msohail

Reputation: 1125

Using bash script to compare two files and write new file

I am new to bash scripting and would like to write a bash script to do the following. I have two tab delimited files as follows:

# file1 #
## position id 
.
.
.


# file2
## position type
. 
.
.

file 2 is a subset of file 1 with regard to the position column. I want to generate file3 such that for every line in file2, it looks up the line with the same position in file1 and writes out the following into file3

position id type
.
.
.

So, I want to essentially find the common positions between file1 and file2 (first column) and write them out along with their id and types (column 2 in the respective files). I know how to do this using python but I would like to learn how to do a procedure like this using a bash script - i would appreciate any example code for the above problem.

Extra note:

The values in each column are as follows

Thanks

Upvotes: 0

Views: 1144

Answers (2)

sehe
sehe

Reputation: 393114

join -a 1 <(sort file1) <(sort file2)

Should get you there

The man page of join has more background and samples:

Upvotes: 4

Pavel
Pavel

Reputation: 7552

first, you need to make the files tab-separated, then it's as easy as

join -j 1 first.txt second.txt

Upvotes: -1

Related Questions