cancel
Showing results for 
Search instead for 
Did you mean: 

Re: Replace in a string

 
SOLVED
Go to solution
Regular Advisor

Replace in a string

Hi


I've this in input

/logs/test/1/xxxx

I'd like to replace
/logs/
by
/logs_backup/

The result must be /logs_backup/test/1/xxxx

How to do this small trick?


Regards
Den
8 REPLIES 8
Regular Advisor

Re: Replace in a string

to be precise

A=/logs/test/1/xxxx
A=Transformed_string($A)


Now A = /logs_backup/test/1/xxxx
mkdir -p $A

Something like that ....
Honored Contributor
Solution

Re: Replace in a string

A=/logs/test
A=$(echo $A | sed 's/logs/logs_backup/')

echo $A
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Regular Advisor

Re: Replace in a string

HUmmm

Is this will replace only the first occurence ?

Bests regards Ivan
Den
Acclaimed Contributor

Re: Replace in a string

Hi Den:

> Is this will replace only the first occurence ?

Well, yes on a line-by-line basis. That is every _line_ in your input file will have the substitution performed _once_.

If you add the 'g' flag to the substitution, then every occurance on every line will be replaced:

# sed 's/logs/logs_backup/g' file

Regards!

...JRF...
Regular Advisor

Re: Replace in a string

Hum

ok. But if the replace string is
/su01/bazar/
instead of /logs_backup/


/logs/test/1/xxxx
=> /su01/bazar/test/1/xxxx

Bests regards
Den
Occasional Advisor

Re: Replace in a string

well, do the same thing, but this time, using the new string...what's wrong with that? have you tried it using sed?
Acclaimed Contributor

Re: Replace in a string

Hi Den:

> ok. But if the replace string is
/su01/bazar/
instead of /logs_backup/

Escape the forward slashes like:

# X=/logs/test/1/xxxx
# echo ${X}|sed -e 's/\/logs/\/su01\/bazar/g'
/su01/bazar/test/1/xxxx

Regards!

...JRF...
Regular Advisor

Re: Replace in a string

Thanks to you Guys
Bests Regards
Den