extracting the corresponding ID from two files in linux
2
0
Entering edit mode
4.9 years ago

Dear all,

Assuming that I have two txt files, File 1 has many rows, each row containing many ID which is separated using "/", like this:

30676/30711/30712/30725/58038/58118
58152/58153/64269/64272/64273/64609/64610
30148/30154/30592/30696/30724
....

And File 2, the Column 1 containing may ID which is same from File1, and the Column 2 is another kind of ID, like this:

  Column1    Column2 
   30711    786673 
   30676    8368 
   58152    037649
   30969    6549
  ....

And what I want to know is if I could using a script, to get File 1's corresponding ID in Column 2, and output it in the same format like file1?

  8368/786673 
  037649
  6549
  ....

File 1 and File 2 are pretty large, so it is impossible that I do it one by one, I will be pretty appreciated if you know how to achieve this using a script.

Thank you!

RNA-Seq • 809 views
ADD COMMENT
1
Entering edit mode
4.9 years ago
Joe 21k

Not 100% sure if I've understood the relation between the ouput and the input, but I'm assuming you've just not shown complete output for brevity.

Is this what you needed?

If slashfile.txt is

30676/30711/30712/30725/58038/58118
58152/58153/64269/64272/64273/64609/64610
30148/30154/30592/30696/30724

and the keys.txt file is:

30711    786673
30676    8368
58152    037649
30969    6549

Formatting can be preserved by just doing 'in place' edits via sed:

First, since this will edit the file directly, make a backup of the first file (cp slashfile.txt slashfile.txt.bak). then:

while IFS=' ' read -r -a array ; do sed -i "s/\b${array[0]}\b/${array[1]}/gi" slashfile.txt ; done < keys.txt

Which produces:

8368/786673/30712/30725/58038/58118
037649/58153/64269/64272/64273/64609/64610
30148/30154/30592/30696/30724

Its not clear to me from the output whether you want the strings without corresponding strings to disappear or not.

ADD COMMENT
0
Entering edit mode

Thank you!! it worked for me !

ADD REPLY
0
Entering edit mode

Great, be sure to accept any answers that worked for you and you can optionally upvote them (and any helpful comments).

ADD REPLY

Login before adding your answer.

Traffic: 2703 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6