So I have this string:
$var=server@10.200.200.20:/home/some/directory/file
I just want to extract the directory address meaning I only wan
This should do the trick:
$ echo "$var" | awk -F':' '{print $NF}'
/home/some/directory/file
This will also do.
echo $var | cut -f2 -d":"
awk -F: '{print $2}' <<< $var
This might work for you:
echo ${var#*:}
See Example 10-10. Pattern matching in parameter substitution
For completeness, using cut
cut -d : -f 2 <<< $var
And using only bash:
IFS=: read a b <<< $var ; echo $b
You don't say which shell you're using. If it's a POSIX-compatible one such as Bash, then parameter expansion can do what you want:
Parameter Expansion
...
${parameter#word}
Remove Smallest Prefix Pattern.
Theword
is expanded to produce a pattern. The parameter expansion then results inparameter
, with the smallest portion of the prefix matched by the pattern deleted.
In other words, you can write
$var="${var#*:}"
which will remove anything matching *:
from $var
(i.e. everything up to and including the first :
). If you want to match up to the last :
, then you could use ##
in place of #
.
This is all assuming that the part to remove does not contain :
(true for IPv4 addresses, but not for IPv6 addresses)