Thank you!! it worked for me !
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!
2 answers
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.
Great, be sure to accept any answers that worked for you and you can optionally upvote them (and any helpful comments).
Thanks for your kindly reply!! I tested using my file, this script can give me an output, but the output file is like:
100303714
100304470
100304491
100304501
100304506
100304507
100304514
If there is a method that we could let the output file just like the format in file1.....every row has many ID, and each ID seperated by "/"....And we don't need to remove replicates, because in file1, one ID can be seen in many different rows..
Thank you so much and looking forward to your reply.
Log in to answer this question.