Newbie -- Unix string substitution problem

Hi everyone,
Apologies if this question is too simple, I'm a newcomer here.

I'm using ksh.  I'm defining a variable, and then I want to replace the occurrence of "string1" in that var to "string2".  Here's how I do it.

a=string1
$a =~ s/string1/string2/g

I get an error message:
ksh: string1:  not found

Please, can you tell me what I'm doing wrong?
Thank you very much...
Evelinka
EvelinkaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulS_IIICommented:
Could you give a more concrete example? It is possible that your string1 is a meta character so you would have to escape it. So If you could provide a real example I might be able to help you
0
EvelinkaAuthor Commented:
Hi, PaulS!

I literally make variable a to be equal to string "string1", for testing.  And then I want to replace "string1" with "string2".

So at the command prompt, I first write

a=string1

And then

$a =~ s/string1/string2/g

So after executing the last command, I would expect $a to be equal to "string2".  However, after executing this command, I get the error ksh: string1:  not found.

Did I manage to clear this up?  Please, let me know.  Thanks!

Evelina

0
PaulS_IIICommented:
OK, I misunderstood. Thanks for clearing it up.

Now are you doing this at the command prompt or in a script?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

EvelinkaAuthor Commented:
Hi again, PaulS,

I'm doing it at the command prompt, not in a script.

Evelina
0
PaulS_IIICommented:
OK, because the syntax you are using is akin to Perl and how you can substitute strings within a variable.

I tried the same syntax here on my Linux box and I also get an error very similar to yours. I do not think you can do that at the command prompt, but I am still working on it.
0
chris_calabreseCommented:
As PaulS_III pointed out, this is the syntax to do this in Perl.

There is no direct equivalent built into ksh as this is usually done via sed as in:
  a="$(print -r -- "$a" | sed 's/string1/string2/g')"

Of course, in this trivial example you could always just do
  a=string2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulS_IIICommented:
Thanks Chris,
I was just going to mention the use of sed, as this is the only way I know of to do that.

But then as Chris mentioned, using sed for something like this is a bit overkill.
0
chris_calabreseCommented:
I don't know if I'd agree that using sed is overkill. Sed is the standard way of doing this type of thing in sh/ksh/csh scripts.

When Perl came along, its designers decided to simply include sed's capabilities directly inside the language (notice the syntax is the same between Perl and sed here).

I'm sure ksh would have adopted this idea eventually, but Dave Korn lost interest in doing this type of updating around 10 years ago.

So, if you're doing a lot of string manipulation, just use Perl.
0
PaulS_IIICommented:
However, there is another way to do this without using sed.

you can use the tr command like so:

1) set the initial value of the variable: a="string1"

2) make sure it was set: echo $a (should be string1)

3) permanently change the value: a=`echo $a | tr 'string1' 'string2'`

4) verify it was changed: echo $a (should now be string2)

Hope that helps

Paul
0
PaulS_IIICommented:
Chris,

I meant that using sed for this type of string manipulation, simple stuff like this there are less confusing ways to alter the value of a variable then with sed.

I use sed for some very complex string manipulation when it comes to files, but not normally with variable substitution.

Sorry for the confusion

Paul
0
chris_calabreseCommented:
Tr translates individual characters, not entire strings. You could use it to translate '1' to '2', but it would do this for all instances of '1', not just ones attached to 'string1'.
0
PaulS_IIICommented:
Try my example tr will translate the entire string or a given character
0
EvelinkaAuthor Commented:
Thanks to both of you...  I'll go ahead and use sed, since I'll probably have to do more than just the trivial string manipulation in this example.  

Also, Chris, if you don't mind me asking, in
a="$(print -r -- "$a" | sed 's/string1/string2/g')"
what does -- mean in the print command?

Thanks!
Evelina
0
PaulS_IIICommented:
Thank you
0
chris_calabreseCommented:
It means not to interpret additional arguments beginning with a '-' as options (i.e., in case $a starts with a '-').

Also, as for tr, this works because the example is so trivial. But it is still working at the character level and will make translations you don't want. For example:
  $ echo string1 fubar1 | tr 'string1' 'string2'
  string2 fubar2

You're literally saying here "translate 's' to 's', 't' to 't', 'r' to 'r', 'i' to 'i', 'n' to 'n', 'g' to 'g', and '1' to 2'
0
EvelinkaAuthor Commented:
Thanks very much, Chris!  Your expertise has helped me a lot! :0)))
0
PaulS_IIICommented:
Point well taken about the tr chris.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.