-1

I have some files containing lines, some of them are similar that shown below:

HETATM 2340  C2  2FN     1      15.566  27.839  11.677  1.00 24.33           C

I need to replace

2FN     1 

to

2FN D   1

so that the final result is:

HETATM 2340  C2  2FN     1      15.566  27.839  11.677  1.00 24.33           C

This is rather easy by using sed command and in the case of you always have the same words to replace

sed 's/2FN     1/2FN D   1/g' input.file > output.file

However, in the case one wants to use variables

A="2FN"
B="1"

in sed command, the result is not what is expected, I suppose due to the multiple spaces in the text to replace.

I tried several ways, such as:

A="2FN"
B="1"
S='     '
G=$(echo "$LIG${S}$LIGN")
sed 's/$G/2FN D   1/g' input.file > output.file

But no expected result has been obtained. Interestingly, by echo G variable is:

"2FN     1" 

but sed doesn't replace to

"2FN D   1"

Do you have any suggestions?

Thanks

danilo
  • 21
  • 1

1 Answers1

0

The problem is you're trying to get bash to resolve variables within single quotes. Single quotes are telling bash: "Don't resolve anything in here, take it literally as is"

If you simply replace the single quotes in your sed command with double quotes, as @oguzismail suggested, you'll be fine.

Much more detail, if needed, is here: https://stackoverflow.com/a/13802438/236528

mjuarez
  • 16,372
  • 11
  • 56
  • 73
  • I didn't pay attention to the double quotes suggested by @oguzismail. Actually, it works by using that . Thanks again – danilo Nov 01 '18 at 17:34