6
./Queen/(1986)/Innuendo/01.One vision.mp3
./Queen/(1986)/Innuendo/02.One vision.mp3
./Queen/(1986)/Innuendo/03.One vision.mp3

I want to display above line as:

01.One vision.mp3
02.One vision.mp3
03.One vision.mp3

I tried with:

  sed -i 's/^[^.$/]/ /g/ 
2
  • 1
    sed -i 's|/|\n/; /\n/D' will progressively chop off chunks before / till the time there's no / left and what remains is the basename of the .mp3s
    – user218374
    Mar 6, 2017 at 10:23
  • Sed -i 's|.*/||' abc.txt it works thanks to everyone Mar 6, 2017 at 12:20

2 Answers 2

11

Your main problem is that you're trying to use / as a separator, when / also is a character you'll be parsing.

You'll need to use a different separator, such as a pipe. Match everything and a /, then use \( and \) (capture groups) delimiting the part you want to extract, and use \1, rendering that group:

echo "yourstring" | sed 's|.*/\([^/]*\)$|\1|'

You may have it working without capture groups: just dropping everything until the last /:

echo "yourstring" | sed 's|.*/||'
1
  • You can also backslash escape the slash in the regex and still use slashes for separators, ie. sed 's/.*\///', but it can be a pain in the ass to read.
    – Oskar Skog
    Mar 6, 2017 at 15:24
6

Why not use basename for this? For example:

for file in *mp3; do basename "$file"; done
1
  • 2
    It appears that OP wants to modify a file in place (see the -i flag in their example). Mar 6, 2017 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.