I am using bash and trying to employ sed command for matching a string and replace it with another between two files.
Objective: To replace all strings[sp_*] by matching them from another file containing [sp_* Var_Names]. Please be noted that: 1. The order is synchronized but not consecutive in both files, so cannot use paste. 2. Functionality is similar to primeKey operations in mysql.
File 1
+--sp_O00574_
|
+--sp_Q9TV16_
|
| +--sp_O18983_
| +--| (52)
| | | +--sp_Q9BDS6_
| | +--| (26)
| | | +--sp_O19024_
| | +--| (29)
| | +--sp_Q9XT45_
File2
O00574 CXCR6_HUMAN
Q9TV16 CXCR6_PANTR
O18983 CXCR6_CHLAE
Q9BDS6 CXCR6_MACFA
O19024 CXCR6_MACNE
Q9XT45 CXCR6_MACMU
Purpose: To sed -ie 's/O00574/CXCR6_HUMAN/g' File1
inline Bash script:
cat File2 | while read id; do upID=`echo $id | cut -d " " -f1`; upName=`echo $id | cut -d " " -f2`; sed -ie 's/sp_$upID/$upName/g' File1; done
Script.sh
#/bin/bash
cat File2 | while read id;
do
upID=`echo $id | cut -d " " -f1`
upName=`echo $id | cut -d " " -f2`
sed -ie 's/sp_$upID/$upName/g' File1
done
Problem: The sed command does not work in the loop. No change is observed in File1, at all. If I echo the sed command from the script and then run it in the terminal it works as expected. I cannot figure out what could be the problem.
Thank you for your valuable comments and solution.