1

I am still new to coding and I am stuck!! I have a file with several hundred unique strings (names) that I would like to replace with unique new strings (useful names). I have the "old" names and "new" names in a csv file with each desired substitution on its own line. The target file to do the substituting is "testtree", which is just a text file. My goal is to come up with a solution that will iterate through each old/new pair and replace every old name with its respective new name in the "testtree" file. I would prefer to keep using bash.

sample_names.csv

myotis4703,M_yumanensis_TK4703
myotis4834,M_californicus_TK4834
myotis7111,M_sodalis_TK7111
myotis10123,M_nigricans_TK10123
myotis10319,M_albescens_TK10319
myotis13147,M_keaysi_TK13147

Here is what I have been trying:

cat sample_names.csv | while read i; do \
    OLD=$(echo ${i} | cut -d, -f1) \
    NEW=$(echo ${i} | cut -d, -f2) \
    sed -i 's/$OLD/$NEW/g' testtree; done

When I try to echo the OLD and NEW variables with this command:

cat sample_names.csv | while read i; do \
    OLD=$(echo ${i} | cut -d, -f1) \
    echo "The old name is" "$OLD"
    NEW=$(echo ${i} | cut -d, -f2) \
    echo "the new name is" "$NEW"; done

I get the following for every entry in my table (sample_names.csv):

The old name is 
the new name is 

Clearly there is an issue with storing the cut output in the OLD and NEW variables. I have tried every combination I can think of of quotes, parentheses, etc., with no success. I would appreciate any suggestions! Thanks!

JennyK
  • 11
  • 1

1 Answers1

0

The mistake is the backslash \ of each line, removing these \

cat sample_names.csv | while read i; do
    OLD=$(echo ${i} | cut -d, -f1)
    echo "The old name is" "$OLD"
    NEW=$(echo ${i} | cut -d, -f2)
    echo "the new name is" "$NEW"; done
Feng
  • 3,592
  • 2
  • 15
  • 14