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 to del all '\'? and assign to oldip_a
Do you have any idea?
To remove the \, use bash parameter substitution.
// means replace all ... (a single / would mean replace only the first)
//\\ means replace all \ (backslash) chars
/} means replace with nothing (There is nothing bewtween / and the closing }
ip="14\.130\.31\.172"
echo "${ip//\\/}"
Output
14.130.31.172
Or, if there are many occurrences \. to do, in a file with many such IP addresses, you can assign each modified value to an array item.
ip=($(printf '
172.31.130.14
14\.130\.31\.172
33\.135\.220\.0
' | sed 's/\\//g'))
for ((i=0;i<${#ip[@]};i++));do
echo "${ip[i]}"
done
output
172.31.130.14
14.130.31.172
33.135.220.0
?! .. I just noticed that your second data item is a reversal of the first.. Is that what you want as your output? (probably not, but it got me wondering)..
ip=('172.31.130.14' '14\.130\.31\.172' '33\.135\.220\.0'); echo "${ip[@]//\\/}"