(Late to the party :)). So many ways to skin this cat...Here are a few:
Here, i am getting the digits surrounded by /
:
With cut
:
cut -d/ -f3
With grep
:
grep -Po '/\K\d+(?=/)'
With perl
:
perl -F/ -lane 'print $F[2]'
With python
:
python3 -c 'import sys; i=sys.stdin.read().split("/"); print(i[2])'
Another sed
:
sed -E 's#.*/([0-9]+)/.*#\1#'
Another awk
(though the already provided awk
answer getting the /
delimited 3rd field should be the way to go):
awk '{i=gensub(".*/([0-9]+)/.*", "\\1", "g"); print i}'
With go
:
package main
import (
"fmt"
"strings"
)
func main() {
str := "cap/media/101/101.mp4"
fmt.Println(strings.Split(str, "/")[2])
}
Example:
% cut -d/ -f3 <<<'cap/media/101/101.mp4'
101
% grep -Po '/\K\d+(?=/)' <<<'cap/media/101/101.mp4'
101
% perl -F/ -lane 'print $F[2]' <<<'cap/media/101/101.mp4'
101
% python3 -c 'import sys; i=sys.stdin.read().split("/"); print(i[2])' <<<'cap/media/101/101.mp4'
101
% sed -E 's#.*/([0-9]+)/.*#\1#' <<<'cap/media/101/101.mp4'
101
% awk '{i=gensub(".*/([0-9]+)/.*", "\\1", "g"); print i}' <<<'cap/media/101/101.mp4'
101
## Go Playground: https://play.golang.org/p/kGdzyywEB2
echo
ed string doesn't relate to your101
thing and yoursed
expression doesn't make sense. What's the actual input and output that you want?/
and.mp4
, or a number between/
and/
, or something else? Because at the moment I would suggestvar=101
. Maybe you could add some more examples? I assume you are not just doing this on one string.101
/ ;