0

Currently I have a file that has a unique line with the pattern

alphanumeric_ChangeMe_moreAlphaNumeric

Actual looks like this:

127.0.0.1 local.com localhost HostType_test_HostNumber2

I'm trying to replace the string test with a variable determined by another command, run as another user using the following code.

site=$(su admin -c get_local_site | less | sed 's/Local Site Name: //')
sed -i -e "s/RecoverPoint_[[:alnum:]]*_RPA/RecoverPoint_$site_RPA/" fakehostfile

I've tested the individual codes and they echo the correct values, but when I try to use the $site variable in the second it fails to replace the section.

I can't seem to find the correct syntax to replace just what's between the underscores with a string (containing only alphanumerics) that's stored in a variable

I've already been looking on here, and found some similar problems, however the solutions don't seem to work, since part of the replacement string is a variable. I've tried to concatenate the string as three separate variables, but it replaces things strange (Maybe due to the underscores?)

What am I missing here??

Questions with similar problems that didn't work:

sed variable replacement does not seem to work Sed replacement not working when using variables

Community
  • 1
  • 1
bubthegreat
  • 301
  • 1
  • 9
  • why're you using the `-e` option? `-e`, if I'm correct, should only be used when you're doing something like this: `sed 's/a/b/' -e '/b/d'`. Also, try using `[a-Z0-9]` instead of `[[:alnum:]]`. – Alexej Magura Apr 07 '14 at 21:22
  • I want to know why bubthegreat is using `| less`, and not just `admin ... | sed 's/...'` ? :-) . `-e` doesn't hurt, but not needed. Good luck to all. – shellter Apr 07 '14 at 21:26
  • Edited out the less that wasn't actually being used - the -e was because it was recommended when I was searching string replace methods for sed, but removed per suggestions. Thanks everybody! – bubthegreat Apr 09 '14 at 14:13

1 Answers1

1

As others recommended, no less command required, and if you need show the line of Local Site Name only, use -n option in Sed.

Second, put varies in braces, it should fix your problem.

site=$(su admin -c get_local_site | sed -n 's/Local Site Name: //p')
sed -i "s/RecoverPoint_[[:alnum:]]*_RPA/RecoverPoint_${site}_RPA/" fakehostfile
BMW
  • 42,880
  • 12
  • 99
  • 116
  • Thanks - The less wasn't actually being used at any point in there, I forgot to edit it out after more troubleshooting. The brackets worked like a charm, thanks! – bubthegreat Apr 09 '14 at 14:12