shell - In a bash script, replace character in string with multiple characters -


i want replace "." in result: "172.16.0.25" " dot ".

here code:

#!/bin/bash     connection=`netstat -tn | grep :1337 | awk '{print $5}' | cut -d: -f1` #this returns "172.16.0.25" replace=" dot " final=${connection/./$replace} echo "$final" 

which returns: test.sh: 4: test.sh: bad substitution

i tried using tr '.' ' dot ' replaced '.' space (' ')

i know dumb question, i'm new shell script.

also, if changes anything, i'm on raspberry pi 2 running raspbian.

you can same awk alone :

netstat -tn | awk '/:1337/{sub(/:.*/,"",$5);gsub(/\./," dot ",$5);print $5}' 

if pattern :1337 matched, take 5th field. remove :number part. replace . dot , print field.


Comments