Question: extracting the corresponding ID from two files in linux
0
gravatar for mxlsherry1992
8 months ago by
mxlsherry199230 wrote:

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 • 179 views
ADD COMMENTlink modified 8 months ago by Joe18k • written 8 months ago by mxlsherry199230
1
gravatar for Joe
8 months ago by
Joe18k
United Kingdom
Joe18k wrote:

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 COMMENTlink written 8 months ago by Joe18k

Thank you!! it worked for me !

ADD REPLYlink written 8 months ago by mxlsherry199230

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

ADD REPLYlink written 8 months ago by Joe18k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2008 users visited in the last hour