0

I have string contains a path

string="toto.titi.1.tata.2.abc.def"

I want to extract the substring which is situated after toto.titi.1.tata.2.. but 1 and 2 here are examples and could be other numbers.

In general: I want to extract the substring which situated after toto.titi.[i].tata.[j]..

[i] and [j] are a numbers

How to do it?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268

7 Answers7

6

Pure bash solution:

[[ $string =~ toto\.titi\.[0-9]+\.tata\.[0-9]+\.(.*$) ]] && result="${BASH_REMATCH[1]}"
echo "$result"
dogbane
  • 266,786
  • 75
  • 396
  • 414
5

An alternate bash solution that uses parameter expansion instead of a regular expression:

echo "${string#toto.titi.[0-9].tata.[0-9].}"

If the numbers can be multi-digit values (i.e., greater than 9), you would need to use an extended pattern:

shopt -s extglob
echo "${string#toto.titi.+([0-9]).tata.+([0-9]).}"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • a good answer (+1). but you can avoid the use of `shopt -s extglob` by using `echo "${string#toto.titi.[0-9]*.tata.[0-9]*.}"` – MOHAMED Jul 26 '13 at 13:53
  • This is a pattern, not a regular expression. `[0-9]*` would match a single digit followed by *anything*, not a (possibly empty) series of digits. – chepner Jul 26 '13 at 13:54
  • For my case I m sure that my string contains only numbers. so for my case it's ok to use `[0-9]*` – MOHAMED Jul 26 '13 at 13:57
  • In that case, you wouldn't even need to specify the numbers; `${string#toto.titi.*.tata.*.}` would probably work (since the `#` operator will match the shortest string possible). – chepner Jul 26 '13 at 14:07
1

You can use cut

 echo $string | cut -f6- -d'.'
jh314
  • 27,144
  • 16
  • 62
  • 82
0

This does it:

echo ${string} | sed -re 's/^toto\.titi\.[[:digit:]]+\.tata\.[[:digit:]]+\.//'
thb
  • 13,796
  • 3
  • 40
  • 68
0

May be like this:

echo "$string" | cut -d '.' -f 6-

phoxis
  • 60,131
  • 14
  • 81
  • 117
0

You can use sed. Like this:

string="toto.titi.1.tata.2.abc.def"
string=$(sed 's/toto\.titi\.[0-9]\.tata\.[0-9]\.//' <<< "$string")
echo "$string"

Output:

abc.def
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • @dogbane I expect that under the hood it is almost the same. But however, I have updated the post – hek2mgl Jul 23 '13 at 13:59
0

try this awk line:

awk -F'toto\\.titi\\.[0-9]+\\.tata\\.[0-9]+\\.' '{print $2}' file

with your example:

kent$  echo "toto.titi.1.tata.2.abc.def"|awk -F'toto\\.titi\\.[0-9]+\\.tata\\.[0-9]+\\.' '{print $2}'
abc.def
Kent
  • 189,393
  • 32
  • 233
  • 301