3

I want to use regex in bash at line of variable assignment

e.g.

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

How to use regex to del all '\' in $oldip? Then assign to new value to $oldip_a.

Do you have any idea?

2 Answers 2

4

I believe you want to use string replacement like this:

    oldip_a=${oldip//\\/}

Or something like that... Of course there's always some battle escaping backslashes!

A more obvious example:

    some_variable=${some_other_variable//replaceEachOfThese/withThis}

search for "replace all matches" on this page:

http://tldp.org/LDP/abs/html/string-manipulation.html
Sign up to request clarification or add additional context in comments.

3 Comments

I think you have a typo (extra / at the end) - should be oldip_a=${oldip//\\/}
That's why I threw in the something like that... Thanks!
No problem. I would have voted up your answer but i ran out of daily votes!
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.