Reputation: 70
I'm currently making a script in Bash to make my life slightly easier at work.
I'm trying to isolate the host name from my nslookup
call, but I'm having some issues.
The output from nslookup
is 1.0.0.10.in-addr.arpa name = nuc3.homenet.no
and I'm trying to match and save only the nuc3
part of the string and save it in a variable, by capturing nuc3
and replacing the whole string with the captured group.
NSLOOKUP=$(nslookup 10.0.0.1)
NUC=$(echo ${NSLOOKUP} | sed -E 's/.*?(nuc\d+).*/\1/'
\d+
is predominantly because there are several machines named the same way with numbers ranging from 1-10.
The output from sed
would just default to printing the original string, so I'd something sketchy is going on. I've successfully used the same method to isolate strings earlier with no problems.
Upvotes: 1
Views: 130
Reputation: 786091
sed
doesn't support \d
. Just use [0-9]
and also match a space before nuc
to avoid false matches:
echo '1.0.0.10.in-addr.arpa name = nuc3.homenet.no' | sed -E 's/.* (nuc[0-9]+).*/\1/'
nuc3
Upvotes: 1
Reputation: 5054
Why not this ?
echo '1.0.0.10.in-addr.arpa name = nuc3.homenet.no' | awk -F'=' '{print $2}' | tr -d -c '[a-zA-Z0-9.\n]'
Or also this :
echo '1.0.0.10.in-addr.arpa name = nuc3.homenet.no' | grep -o -P '[a-zA-Z]+[0-9]+[.][a-zA-Z]+[.][a-zA-Z]+'
Upvotes: 1