Hello,
I am processing text files looking for a string and replacing the first occurrence of the string with something else.
For the text,
id Name
1 methyl-(2-methylpropoxy)-oxoammonium
2 N-amino-N-(methylamino)-2-nitrosoethanamine
3 3-methoxy-3-methyloxazolidin-3-ium
4 1,3-dihydroxypropan-2-yl-methyl-methyleneammonium
5 (1R)-1,2,3,3-tetraamino-2-propen-1-ol
6 2-(ethoxyamino)guanidine
7 O-[(2S)-2-aminoazopropyl]hydroxylamine
8 N-$l^{1}-oxidanyl-N-[(2-methylpropan-2-yl)oxy]methanamine
9 (1R)-1,2,3,3-tetraamino-2-propen-1-ol
10 1-amino-1-ethoxyguanidine
I am replacing the first instance of (1R)-1,2,3,3-tetraamino-2-propen-1-ol
with 0_(1R)-1,2,3,3-tetraamino-2-propen-1-ol
If I do the following in sed,
sed '0,/(1R)-1,2,3,3-tetraamino-2-propen-1-ol/s//0_(1R)-1,2,3,3-tetraamino-2-propen-1-ol/' input > output.txt
I get the necessary results.
If I add variables to the command line,
current_name="(1R)-1,2,3,3-tetraamino-2-propen-1-ol";
new_name="0_(1R)-1,2,3,3-tetraamino-2-propen-1-ol";
sed -e "0,/$current_name/s//$new_name/" input > output.txt
I still get the necessary results. When, however, I assign current_name and new_name from a bash array and other bash variables,
current_name="${FIELD[1]}"
new_name='dup_'$name_count'_'$current_name
I do not get the modified output and the file is unchanged. Apparently sed is not able to match the pattern in the file. There are any number of non-standard characters in the data so I don't know if that is an issue or not. The difference that I can see is that when I assign new_name="0_(1R)-1,2,3,3-tetraamino-2-propen-1-ol"
, I am able to quote the string but when I assign current_name="${FIELD[1]}"
I am not able to quote/escape special characters like ( in the string.
It seems like I just am missing some combination of single and double quotes to do the job but I haven't been able to progress past this.
Suggestions would be appreciated.
LMHmedchem