Replace multiple text with corresponding text
1
0
Entering edit mode
3.3 years ago
fec2 ▴ 50

Hi,

I run an analysis and the software replaced the bacteria name with codes, and I have txt file as below:

Order   Original Name   Code
1   Allostreptomyces_psammosilenae_DSM_42178    S1_f1
2   Embleya_hyalina_NBRC_13850  S2_f2
3   Embleya_scabrispora_DSM_41855   S3_f3

Because the analysis involved few hundreds bacteria, it would be difficult to replace it one by one in the output txt file. May I know any method for me replace the code back to my bacteria original name? Thanks in advance.

Best regards,

Felix

replacement text • 1.7k views
ADD COMMENT
0
Entering edit mode
3.3 years ago
Joe 21k

This is a perfect task for sed.

I would suggest modifying the file in to a set of sed commands. Assume your input above is called subs.txt:

awk 'BEGIN {OFS=","} {print $3, $2}' subs.txt | tail -n+2 | sed -e "s|^|s\/|" -e "s|,|\/|" -e "s|$|\/g|" > changes.sed

Given your input, this will create:

s/S1_f1/Allostreptomyces_psammosilenae_DSM_42178/g
s/S2_f2/Embleya_hyalina_NBRC_13850/g
s/S3_f3/Embleya_scabrispora_DSM_41855/g

You can then feed this in as a sed script to alter your original file:

sed -f changes.sed file_to_be_changed.txt
ADD COMMENT
0
Entering edit mode

Hi, the changes.sed file is empty. Anyway I am using Macbook, I have also tried using gawk and gsed but the changes.sed file still empty.

ADD REPLY
0
Entering edit mode

Mac implements sed etc slightly differently. It may also do the same for awk. Try building the pipe up part by part.

I'm afraid that's not enough information for me to be able to tell you why it isn't working

ADD REPLY
0
Entering edit mode

Hi, I generated the changes.sed file using excel and save as .txt file as below:

s/S1_f1/Allostreptomyces_psammosilenae_DSM_42178/g
s/S2_f2/Embleya_hyalina_NBRC_13850/g
s/S3_f3/Embleya_scabrispora_DSM_41855/g

But when run the second command I got error as below:

sed: file changes.txt line 1: unknown option to `s'

Any suggestion?

ADD REPLY
0
Entering edit mode

Make sure you are using GNU sed as distributed in coreutils.

ADD REPLY
0
Entering edit mode

I have changed my command to gsed, but result remain the same.

ADD REPLY
0
Entering edit mode

It may be due to you producing the file with Excel. How did you export the file? Any invisible characters will break this.

ADD REPLY
0
Entering edit mode

I saved the file as tab delimitated text file.

ADD REPLY
0
Entering edit mode

OK, so how did you make the file you showed above? did you use the sed commands I gave you on the tab-separated file?

ADD REPLY
0
Entering edit mode

Sorry, I don't know why the command is not working on my side. But still, thanks for your sed command, I manually run all the sed command one by one using && in the terminal.

ADD REPLY

Login before adding your answer.

Traffic: 2369 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