0

I have this String cap/media/101/101.mp4 and I need to cut the 101 in another Variable.

I am trying to do it with sed but it's not giving me the output I need.

echo cap/media/101/101.mp4 | sed -e 's/d/\(.*\)/\1/'

can someone please give an idea about it?

8
  • I am confused. Your echoed string doesn't relate to your 101 thing and your sed expression doesn't make sense. What's the actual input and output that you want?
    – Zanna
    Feb 8, 2017 at 13:34
  • @zanna, sorry that was my mistake.
    – kunal
    Feb 8, 2017 at 13:37
  • no problem, but I still don't quite understand your challenge - which part of the string do you want to keep? The part between the last / and .mp4, or a number between / and /, or something else? Because at the moment I would suggest var=101. Maybe you could add some more examples? I assume you are not just doing this on one string.
    – Zanna
    Feb 8, 2017 at 13:41
  • @Zanna, yes i have a txt file with Multiple line with similar kind of string in it. and I would need to cut the part which is after media/ 101 / ;
    – kunal
    Feb 8, 2017 at 13:42
  • please could you give a larger input and output sample so we can see what you want? :)
    – Zanna
    Feb 8, 2017 at 13:43

3 Answers 3

2

Assuming consistent fields, a simple awk command can do the trick:

echo cap/media/101/101.mp4 | awk -F"/" '{print $3}'

Output:

101
1

Assuming you want the characters between media/ and the next / (based on what you said in the comments) you could cut that part with

sed -r 's|.*media/([^/]+).*|\1|'

Explanation

  • -r use ERE (so we don't have to escape () or + to use them as metacharacters
  • s|old|new| replace old with new (using an alternative delimiter to avoid having to escape / in the string)
  • ([^/]+) save some characters that are not /
  • \1 the saved pattern (the rest is discarded)
9
  • @kunal always a pleasure
    – Zanna
    Feb 8, 2017 at 14:05
  • I am using this Sed Command Into the Bash script and can you please tell me how can i hold the value of after Sed into varibale
    – kunal
    Feb 8, 2017 at 14:35
  • hmm could you paste your script into your questions or at paste.ubuntu.com ? @kunal
    – Zanna
    Feb 8, 2017 at 14:37
  • I have pasted it
    – kunal
    Feb 8, 2017 at 14:39
  • we will need the exact link though
    – Zanna
    Feb 8, 2017 at 14:42
1

(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
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .