How to assign value to variable with regex (bash)?

I want to use regex in bash on variable assignment string

eg.

oldip="14\.130\.31\.172"
oldip_a="14.130.31.172" //How to use regex on this line.

      

How do I use regex for all '\' in $ oldip? Then set the new value to $ oldip_a.

Do you have any ideas?

+3


source to share


2 answers


I believe you want to use string replacement like this:

    oldip_a=${oldip//\\/}

      

Or something like that ... Of course, always the battle escapes the backslash!

A more obvious example:



    some_variable=${some_other_variable//replaceEachOfThese/withThis}

      

find "replace all matches" on this page:

http://tldp.org/LDP/abs/html/string-manipulation.html

      

+4


source


Here's how you can do it:

oldip="14\.130\.31\.172"
oldip_a=`echo $oldip | sed 's/[\]//g'`
echo $oldip_a

      



OUTPUT

14.130.31.172

      

+2


source







All Articles