39

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.

canecse
  • 1,772
  • 3
  • 16
  • 20

7 Answers7

51

Using sed:

$ var=server@10.200.200.20:/home/some/directory/file
$ echo $var | sed 's/.*://'
/home/some/directory/file
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 3
    what if the original string had more than one `:` character? Like `$var=server@10.200.200.20:administrators:/home/some/directory/file`. How could we obtain only the last field (the path data)? – Sopalajo de Arrierez Feb 20 '15 at 23:13
  • 3
    @SopalajodeArrierez, The given command will just work. See http://asciinema.org/a/16807 (because the `.*` will match as much as possible: greedy) – falsetru Feb 21 '15 at 07:05
  • I like sed commands, which is easier to read! – dave Mar 29 '22 at 18:57
31

This might work for you:

echo ${var#*:}

See Example 10-10. Pattern matching in parameter substitution

potong
  • 55,640
  • 6
  • 51
  • 83
  • 1
    +1 Don't mess with cryptic external utilities when the shell has built-in syntax for this. Maybe explore `"${var##*:}"` if you don't need to be portable to older shells. But don't omit the double quotes around variable interpolations unless you are specifically looking for whitespace tokenization. – tripleee Sep 06 '13 at 05:43
  • @tripleee Nitpicking a little here...s/older shells/non-POSIX shells/. Parameter expansion including `${var#...}` and `${var##...}` is defined by POSIX. – Adrian Frühwirth Sep 06 '13 at 05:57
  • 7
    Pretty useless answer if you don't explain how it works. Explanation can be found here: http://stackoverflow.com/a/19482947/1220835 – Basti May 31 '16 at 11:42
  • Note: If there is no match, here no semi-column, then there is no replacement. – mcoolive Oct 04 '21 at 11:48
25

This will also do.

echo $var | cut -f2 -d":"
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Manish V
  • 267
  • 2
  • 2
12

For completeness, using cut

cut -d : -f 2 <<< $var

And using only bash:

IFS=: read a b <<< $var ; echo $b
chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
9

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.
The word is expanded to produce a pattern. The parameter expansion then results in parameter, 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)

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
7

This should do the trick:

$ echo "$var" | awk -F':' '{print $NF}'
/home/some/directory/file
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
3
awk -F: '{print $2}' <<< $var
Jotne
  • 40,548
  • 12
  • 51
  • 55