Replace multiple text with corresponding text

Replace multiple text with corresponding text

1

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

• 98 views

26 minutes ago by


fec2

▴

30

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


Login
before adding your answer.

Source link