canecse canecse - 3 months ago 36
Bash Question

How to cut a string after a specific character in unix

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 want the bit after the ":" character and get:

/home/some/directory/file


thanks.

I need a generic command so the cut command wont work as the $var variable doesn't have a fixed length.

Answer

Using sed:

$ var=server@10.200.200.20:/home/some/directory/file
$ echo $var | sed 's/.*://'
/home/some/directory/file