Cutting all the characters after the last /

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
8
down vote

favorite
1












How do I cut all characters after the last '/'?



This text



xxxx/x/xx/xx/xxxx/x/yyyyy
xxx/xxxx/xxxxx/yyy


should return



xxxx/x/xx/xx/xxxx/x
xxx/xxxx/xxxxx









share|improve this question



















  • 4




    Are you trying to omit or isolate the characters after the last /?
    – dsstorefile1
    Feb 27 at 15:34










  • Good question, I just realised that we didn't understand the question the same way
    – Félicien
    Feb 27 at 15:35






  • 3




    The commands basename and dirname do this already.
    – Michael Hampton
    Feb 28 at 0:49










  • I don't think edit #5 to the question was correct. Using the word "cut" might be ambiguous; if you cut something, do you throw away that part, or do you keep only that? However, the previous wording "and to omit what is before the /" was unambiguous. This was changed to its opposite.
    – egmont
    Mar 1 at 0:02










  • @egmont the word "cut" itself was carried over from the original: "I need to cut only the charachters after the last /" became "How do I cut all characters after the last '/'?" which is a change of phrasing that reasonably keeps the meaning, IMO. If anything, I felt the "omit what is ..." part ambiguous: the examples seem to keep what is before the /. The example, makes it clear, anyway.
    – muru
    Mar 1 at 4:30















up vote
8
down vote

favorite
1












How do I cut all characters after the last '/'?



This text



xxxx/x/xx/xx/xxxx/x/yyyyy
xxx/xxxx/xxxxx/yyy


should return



xxxx/x/xx/xx/xxxx/x
xxx/xxxx/xxxxx









share|improve this question



















  • 4




    Are you trying to omit or isolate the characters after the last /?
    – dsstorefile1
    Feb 27 at 15:34










  • Good question, I just realised that we didn't understand the question the same way
    – Félicien
    Feb 27 at 15:35






  • 3




    The commands basename and dirname do this already.
    – Michael Hampton
    Feb 28 at 0:49










  • I don't think edit #5 to the question was correct. Using the word "cut" might be ambiguous; if you cut something, do you throw away that part, or do you keep only that? However, the previous wording "and to omit what is before the /" was unambiguous. This was changed to its opposite.
    – egmont
    Mar 1 at 0:02










  • @egmont the word "cut" itself was carried over from the original: "I need to cut only the charachters after the last /" became "How do I cut all characters after the last '/'?" which is a change of phrasing that reasonably keeps the meaning, IMO. If anything, I felt the "omit what is ..." part ambiguous: the examples seem to keep what is before the /. The example, makes it clear, anyway.
    – muru
    Mar 1 at 4:30













up vote
8
down vote

favorite
1









up vote
8
down vote

favorite
1






1





How do I cut all characters after the last '/'?



This text



xxxx/x/xx/xx/xxxx/x/yyyyy
xxx/xxxx/xxxxx/yyy


should return



xxxx/x/xx/xx/xxxx/x
xxx/xxxx/xxxxx









share|improve this question















How do I cut all characters after the last '/'?



This text



xxxx/x/xx/xx/xxxx/x/yyyyy
xxx/xxxx/xxxxx/yyy


should return



xxxx/x/xx/xx/xxxx/x
xxx/xxxx/xxxxx






command-line text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 28 at 3:57









muru

130k19274467




130k19274467










asked Feb 27 at 15:27









Josef Klimuk

542112




542112







  • 4




    Are you trying to omit or isolate the characters after the last /?
    – dsstorefile1
    Feb 27 at 15:34










  • Good question, I just realised that we didn't understand the question the same way
    – Félicien
    Feb 27 at 15:35






  • 3




    The commands basename and dirname do this already.
    – Michael Hampton
    Feb 28 at 0:49










  • I don't think edit #5 to the question was correct. Using the word "cut" might be ambiguous; if you cut something, do you throw away that part, or do you keep only that? However, the previous wording "and to omit what is before the /" was unambiguous. This was changed to its opposite.
    – egmont
    Mar 1 at 0:02










  • @egmont the word "cut" itself was carried over from the original: "I need to cut only the charachters after the last /" became "How do I cut all characters after the last '/'?" which is a change of phrasing that reasonably keeps the meaning, IMO. If anything, I felt the "omit what is ..." part ambiguous: the examples seem to keep what is before the /. The example, makes it clear, anyway.
    – muru
    Mar 1 at 4:30













  • 4




    Are you trying to omit or isolate the characters after the last /?
    – dsstorefile1
    Feb 27 at 15:34










  • Good question, I just realised that we didn't understand the question the same way
    – Félicien
    Feb 27 at 15:35






  • 3




    The commands basename and dirname do this already.
    – Michael Hampton
    Feb 28 at 0:49










  • I don't think edit #5 to the question was correct. Using the word "cut" might be ambiguous; if you cut something, do you throw away that part, or do you keep only that? However, the previous wording "and to omit what is before the /" was unambiguous. This was changed to its opposite.
    – egmont
    Mar 1 at 0:02










  • @egmont the word "cut" itself was carried over from the original: "I need to cut only the charachters after the last /" became "How do I cut all characters after the last '/'?" which is a change of phrasing that reasonably keeps the meaning, IMO. If anything, I felt the "omit what is ..." part ambiguous: the examples seem to keep what is before the /. The example, makes it clear, anyway.
    – muru
    Mar 1 at 4:30








4




4




Are you trying to omit or isolate the characters after the last /?
– dsstorefile1
Feb 27 at 15:34




Are you trying to omit or isolate the characters after the last /?
– dsstorefile1
Feb 27 at 15:34












Good question, I just realised that we didn't understand the question the same way
– Félicien
Feb 27 at 15:35




Good question, I just realised that we didn't understand the question the same way
– Félicien
Feb 27 at 15:35




3




3




The commands basename and dirname do this already.
– Michael Hampton
Feb 28 at 0:49




The commands basename and dirname do this already.
– Michael Hampton
Feb 28 at 0:49












I don't think edit #5 to the question was correct. Using the word "cut" might be ambiguous; if you cut something, do you throw away that part, or do you keep only that? However, the previous wording "and to omit what is before the /" was unambiguous. This was changed to its opposite.
– egmont
Mar 1 at 0:02




I don't think edit #5 to the question was correct. Using the word "cut" might be ambiguous; if you cut something, do you throw away that part, or do you keep only that? However, the previous wording "and to omit what is before the /" was unambiguous. This was changed to its opposite.
– egmont
Mar 1 at 0:02












@egmont the word "cut" itself was carried over from the original: "I need to cut only the charachters after the last /" became "How do I cut all characters after the last '/'?" which is a change of phrasing that reasonably keeps the meaning, IMO. If anything, I felt the "omit what is ..." part ambiguous: the examples seem to keep what is before the /. The example, makes it clear, anyway.
– muru
Mar 1 at 4:30





@egmont the word "cut" itself was carried over from the original: "I need to cut only the charachters after the last /" became "How do I cut all characters after the last '/'?" which is a change of phrasing that reasonably keeps the meaning, IMO. If anything, I felt the "omit what is ..." part ambiguous: the examples seem to keep what is before the /. The example, makes it clear, anyway.
– muru
Mar 1 at 4:30











7 Answers
7






active

oldest

votes

















up vote
10
down vote



accepted










If you want to get the "cutted part"



yyy
yyyyy


You can use



sed 's|.*/||' 


eg.



echo "xxx/xxxx/xxxxx/yyy" | sed 's|.*/||'
echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|.*/||'


output



yyy
yyyyy


(Note: This uses the ability of sed to use a separator other than /, in this case |, in the s command)




If you want to get the begining of the string :



xxx/xxxx/xxxxx
xxxx/x/xx/xx/xxxx/x


You can use



sed 's|(.*)/.*|1|'


eg.



echo "xxx/xxxx/xxxxx/yyy" | sed 's|(.*)/.*|1|'
echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|(.*)/.*|1|'


output



xxx/xxxx/xxxxx
xxxx/x/xx/xx/xxxx/x





share|improve this answer


















  • 5




    Hi, in this case you can change the delimiter, for example # instead of /. Also you can use the option -r if you don't want to escape ` ` each special character: sed -r 's#(^.*)/.*$#1#'.
    – pa4080
    Feb 27 at 15:49







  • 1




    I have submitted a proposed edit to use | instead of /. If you don't like it, I suggest you "reject and edit" it, and change "begginning" to "beginning". (Reject+Edit, because that way it won't get approved by robo-approvers. Alternatively, just roll the change back if it has got approved and you didn't like it.)
    – Martin Bonner
    Mar 1 at 6:33

















up vote
18
down vote













If you're cutting off the ends of the strings,dirname might fit the bill:



$ dirname xxxx/x/xx/xx/xxxx/x/yyyyy
xxxx/x/xx/xx/xxxx/x
$ _


If you're trying to isolate the last part of the string, use echo /$(basename "$str").



$ str=xxxx/x/xx/xx/xxxx/x/yyyyy
$ echo /$(basename "$str")
/yyyyy
$ _





share|improve this answer


















  • 2




    Honestly, why anyone would either suggest or accept an answer involving sed (or awk, or any other similar method) instead of just using the utilities installed with the OS is beyond me.
    – Auspex
    Feb 28 at 11:48










  • @Auspex I'd guess it's simply because dirname and basename are not widely known, but sed and awk are.
    – Volker Siegel
    Feb 28 at 13:08

















up vote
15
down vote













Parameter expansion in bash



You can use parameter expansion in bash, in this case




  • $parameter%word where word is /*


  • $parameter##word where word is */

Examples:



Remove the last part



$ asdf="xxx/xxxx/xxxxx/yyy"
$ echo $asdf%/*
xxx/xxxx/xxxxx


This is described in man bash:



$parameter%word
$parameter%%word
Remove matching suffix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
a trailing portion of the expanded value of parameter, then the
result of the expansion is the expanded value of parameter with
the shortest matching pattern (the ``%'' case) or the longest
matching pattern (the ``%%'' case) deleted. If parameter is @
or *, the pattern removal operation is applied to each posi‐
tional parameter in turn, and the expansion is the resultant
list. If parameter is an array variable subscripted with @ or
*, the pattern removal operation is applied to each member of
the array in turn, and the expansion is the resultant list.


Remove all except the last part



$ asdf="xxx/xxxx/xxxxx/yyy"
$ echo $asdf##*/
yyy


You can add a slash like so



$ echo /$asdf##*/
/yyy


to get exactly what you wanted at one particular instance according to the edited question. But the question has been edited by several people after that and it is not easy to know what you want now.



This is described in man bash:



$parameter#word
$parameter##word
Remove matching prefix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
the beginning of the value of parameter, then the result of the
expansion is the expanded value of parameter with the shortest
matching pattern (the ``#'' case) or the longest matching pat‐
tern (the ``##'' case) deleted. If parameter is @ or *, the
pattern removal operation is applied to each positional parame‐
ter in turn, and the expansion is the resultant list. If param‐
eter is an array variable subscripted with @ or *, the pattern
removal operation is applied to each member of the array in
turn, and the expansion is the resultant list.





share|improve this answer





























    up vote
    9
    down vote













    Another way is to use grep to only display the last slash per line and whatever follows it:



    $ grep -o '/[^/]*$' example.txt
    /yyy
    /yyyyy


    Explanation:



    -o tells grep to only show the matching part of the line instead of the whole line.



    The pattern /[^/]*$ matches a literal slash / followed by any character except for a slash [^/] any number of times * until the end of the line $.






    share|improve this answer



























      up vote
      7
      down vote













      Just because others have posted more “sane” answers, here’s a somewhat silly one:



      rev | cut -d/ -f1 | rev


      rev reverses the characters in each line, e.g. abcd/ef/g becomes g/fe/dcba. Then cut cuts out the first segment. Finally it’s reversed again.






      share|improve this answer
















      • 1




        Despite the non-sanity of this answer, especially because of the shell plumbing, I like this answer :) Keep on rocking, +1
        – Sergiy Kolodyazhnyy
        Feb 28 at 2:32










      • Agreed - not sane, but badass!
        – Volker Siegel
        Feb 28 at 12:12

















      up vote
      3
      down vote













      Classic solution with awk, that treats / as field separator for both input and output and sets the last field to empty string ( which really is "dereferencing" of the number of fields NF variable ):



      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";$NF="";print $0'
      xxx/xxxx/xxxxx/


      Shorter, as fedorqui pointed out in the comments:



      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";NF--'
      xxx/xxxx/xxxxx/


      Variation on that would be to put path into awk's execution environment, which will save up on plumbing but makes awk portion more verbose:



      mypath="xxx/xxxx/xxxxx/yyy" awk 'BEGINOFS=FS="/"; sub(//([^/]*)$/,"",ENVIRON["mypath"]);print(ENVIRON["mypath"]) ;'





      share|improve this answer


















      • 1




        Too verbose, why not just NF--? This way you avoud the print bla bla.
        – fedorqui
        Feb 28 at 9:35


















      up vote
      2
      down vote













      Adding to egmont's answer because the question has been edited...



      Use --complement if you want to remove the first field with -f1.



      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1 --complement|rev
      xxxx/x/xx/xx/xxxx/x

      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1|rev
      yyyyy


      Also, the question isn't quite clear about what should happen with inputs not containing any slashes.
      xxxx => xxxx
      or
      xxxx => nothing






      share|improve this answer






















      • You can suggest an edit to it: askubuntu.com/posts/1010356/edit
        – muru
        Feb 28 at 14:08










      • @muru OP is 1 rep user. Isn't editing privilege is granted at 2k ? askubuntu.com/help/privileges/edit
        – Sergiy Kolodyazhnyy
        Feb 28 at 18:47










      • @SergiyKolodyazhnyy anybody can suggest edits.
        – muru
        Feb 28 at 21:26










      • +1 - I had not noticed the --complement option before and the man page is circular. Complement means complement. I found a nice cut tutorial which covers it at yourownlinux.com/2015/05/… It's like -v in grep. Give me everything except what was matched.
        – Joe
        Feb 28 at 23:59







      • 1




        -f2- is a simpler form for -f1 --complement.
        – egmont
        Mar 1 at 0:04










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1010310%2fcutting-all-the-characters-after-the-last%23new-answer', 'question_page');

      );

      Post as a guest






























      7 Answers
      7






      active

      oldest

      votes








      7 Answers
      7






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      10
      down vote



      accepted










      If you want to get the "cutted part"



      yyy
      yyyyy


      You can use



      sed 's|.*/||' 


      eg.



      echo "xxx/xxxx/xxxxx/yyy" | sed 's|.*/||'
      echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|.*/||'


      output



      yyy
      yyyyy


      (Note: This uses the ability of sed to use a separator other than /, in this case |, in the s command)




      If you want to get the begining of the string :



      xxx/xxxx/xxxxx
      xxxx/x/xx/xx/xxxx/x


      You can use



      sed 's|(.*)/.*|1|'


      eg.



      echo "xxx/xxxx/xxxxx/yyy" | sed 's|(.*)/.*|1|'
      echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|(.*)/.*|1|'


      output



      xxx/xxxx/xxxxx
      xxxx/x/xx/xx/xxxx/x





      share|improve this answer


















      • 5




        Hi, in this case you can change the delimiter, for example # instead of /. Also you can use the option -r if you don't want to escape ` ` each special character: sed -r 's#(^.*)/.*$#1#'.
        – pa4080
        Feb 27 at 15:49







      • 1




        I have submitted a proposed edit to use | instead of /. If you don't like it, I suggest you "reject and edit" it, and change "begginning" to "beginning". (Reject+Edit, because that way it won't get approved by robo-approvers. Alternatively, just roll the change back if it has got approved and you didn't like it.)
        – Martin Bonner
        Mar 1 at 6:33














      up vote
      10
      down vote



      accepted










      If you want to get the "cutted part"



      yyy
      yyyyy


      You can use



      sed 's|.*/||' 


      eg.



      echo "xxx/xxxx/xxxxx/yyy" | sed 's|.*/||'
      echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|.*/||'


      output



      yyy
      yyyyy


      (Note: This uses the ability of sed to use a separator other than /, in this case |, in the s command)




      If you want to get the begining of the string :



      xxx/xxxx/xxxxx
      xxxx/x/xx/xx/xxxx/x


      You can use



      sed 's|(.*)/.*|1|'


      eg.



      echo "xxx/xxxx/xxxxx/yyy" | sed 's|(.*)/.*|1|'
      echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|(.*)/.*|1|'


      output



      xxx/xxxx/xxxxx
      xxxx/x/xx/xx/xxxx/x





      share|improve this answer


















      • 5




        Hi, in this case you can change the delimiter, for example # instead of /. Also you can use the option -r if you don't want to escape ` ` each special character: sed -r 's#(^.*)/.*$#1#'.
        – pa4080
        Feb 27 at 15:49







      • 1




        I have submitted a proposed edit to use | instead of /. If you don't like it, I suggest you "reject and edit" it, and change "begginning" to "beginning". (Reject+Edit, because that way it won't get approved by robo-approvers. Alternatively, just roll the change back if it has got approved and you didn't like it.)
        – Martin Bonner
        Mar 1 at 6:33












      up vote
      10
      down vote



      accepted







      up vote
      10
      down vote



      accepted






      If you want to get the "cutted part"



      yyy
      yyyyy


      You can use



      sed 's|.*/||' 


      eg.



      echo "xxx/xxxx/xxxxx/yyy" | sed 's|.*/||'
      echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|.*/||'


      output



      yyy
      yyyyy


      (Note: This uses the ability of sed to use a separator other than /, in this case |, in the s command)




      If you want to get the begining of the string :



      xxx/xxxx/xxxxx
      xxxx/x/xx/xx/xxxx/x


      You can use



      sed 's|(.*)/.*|1|'


      eg.



      echo "xxx/xxxx/xxxxx/yyy" | sed 's|(.*)/.*|1|'
      echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|(.*)/.*|1|'


      output



      xxx/xxxx/xxxxx
      xxxx/x/xx/xx/xxxx/x





      share|improve this answer














      If you want to get the "cutted part"



      yyy
      yyyyy


      You can use



      sed 's|.*/||' 


      eg.



      echo "xxx/xxxx/xxxxx/yyy" | sed 's|.*/||'
      echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|.*/||'


      output



      yyy
      yyyyy


      (Note: This uses the ability of sed to use a separator other than /, in this case |, in the s command)




      If you want to get the begining of the string :



      xxx/xxxx/xxxxx
      xxxx/x/xx/xx/xxxx/x


      You can use



      sed 's|(.*)/.*|1|'


      eg.



      echo "xxx/xxxx/xxxxx/yyy" | sed 's|(.*)/.*|1|'
      echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|(.*)/.*|1|'


      output



      xxx/xxxx/xxxxx
      xxxx/x/xx/xx/xxxx/x






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 1 at 8:40









      Martin Bonner

      1034




      1034










      answered Feb 27 at 15:30









      Félicien

      8471516




      8471516







      • 5




        Hi, in this case you can change the delimiter, for example # instead of /. Also you can use the option -r if you don't want to escape ` ` each special character: sed -r 's#(^.*)/.*$#1#'.
        – pa4080
        Feb 27 at 15:49







      • 1




        I have submitted a proposed edit to use | instead of /. If you don't like it, I suggest you "reject and edit" it, and change "begginning" to "beginning". (Reject+Edit, because that way it won't get approved by robo-approvers. Alternatively, just roll the change back if it has got approved and you didn't like it.)
        – Martin Bonner
        Mar 1 at 6:33












      • 5




        Hi, in this case you can change the delimiter, for example # instead of /. Also you can use the option -r if you don't want to escape ` ` each special character: sed -r 's#(^.*)/.*$#1#'.
        – pa4080
        Feb 27 at 15:49







      • 1




        I have submitted a proposed edit to use | instead of /. If you don't like it, I suggest you "reject and edit" it, and change "begginning" to "beginning". (Reject+Edit, because that way it won't get approved by robo-approvers. Alternatively, just roll the change back if it has got approved and you didn't like it.)
        – Martin Bonner
        Mar 1 at 6:33







      5




      5




      Hi, in this case you can change the delimiter, for example # instead of /. Also you can use the option -r if you don't want to escape ` ` each special character: sed -r 's#(^.*)/.*$#1#'.
      – pa4080
      Feb 27 at 15:49





      Hi, in this case you can change the delimiter, for example # instead of /. Also you can use the option -r if you don't want to escape ` ` each special character: sed -r 's#(^.*)/.*$#1#'.
      – pa4080
      Feb 27 at 15:49





      1




      1




      I have submitted a proposed edit to use | instead of /. If you don't like it, I suggest you "reject and edit" it, and change "begginning" to "beginning". (Reject+Edit, because that way it won't get approved by robo-approvers. Alternatively, just roll the change back if it has got approved and you didn't like it.)
      – Martin Bonner
      Mar 1 at 6:33




      I have submitted a proposed edit to use | instead of /. If you don't like it, I suggest you "reject and edit" it, and change "begginning" to "beginning". (Reject+Edit, because that way it won't get approved by robo-approvers. Alternatively, just roll the change back if it has got approved and you didn't like it.)
      – Martin Bonner
      Mar 1 at 6:33












      up vote
      18
      down vote













      If you're cutting off the ends of the strings,dirname might fit the bill:



      $ dirname xxxx/x/xx/xx/xxxx/x/yyyyy
      xxxx/x/xx/xx/xxxx/x
      $ _


      If you're trying to isolate the last part of the string, use echo /$(basename "$str").



      $ str=xxxx/x/xx/xx/xxxx/x/yyyyy
      $ echo /$(basename "$str")
      /yyyyy
      $ _





      share|improve this answer


















      • 2




        Honestly, why anyone would either suggest or accept an answer involving sed (or awk, or any other similar method) instead of just using the utilities installed with the OS is beyond me.
        – Auspex
        Feb 28 at 11:48










      • @Auspex I'd guess it's simply because dirname and basename are not widely known, but sed and awk are.
        – Volker Siegel
        Feb 28 at 13:08














      up vote
      18
      down vote













      If you're cutting off the ends of the strings,dirname might fit the bill:



      $ dirname xxxx/x/xx/xx/xxxx/x/yyyyy
      xxxx/x/xx/xx/xxxx/x
      $ _


      If you're trying to isolate the last part of the string, use echo /$(basename "$str").



      $ str=xxxx/x/xx/xx/xxxx/x/yyyyy
      $ echo /$(basename "$str")
      /yyyyy
      $ _





      share|improve this answer


















      • 2




        Honestly, why anyone would either suggest or accept an answer involving sed (or awk, or any other similar method) instead of just using the utilities installed with the OS is beyond me.
        – Auspex
        Feb 28 at 11:48










      • @Auspex I'd guess it's simply because dirname and basename are not widely known, but sed and awk are.
        – Volker Siegel
        Feb 28 at 13:08












      up vote
      18
      down vote










      up vote
      18
      down vote









      If you're cutting off the ends of the strings,dirname might fit the bill:



      $ dirname xxxx/x/xx/xx/xxxx/x/yyyyy
      xxxx/x/xx/xx/xxxx/x
      $ _


      If you're trying to isolate the last part of the string, use echo /$(basename "$str").



      $ str=xxxx/x/xx/xx/xxxx/x/yyyyy
      $ echo /$(basename "$str")
      /yyyyy
      $ _





      share|improve this answer














      If you're cutting off the ends of the strings,dirname might fit the bill:



      $ dirname xxxx/x/xx/xx/xxxx/x/yyyyy
      xxxx/x/xx/xx/xxxx/x
      $ _


      If you're trying to isolate the last part of the string, use echo /$(basename "$str").



      $ str=xxxx/x/xx/xx/xxxx/x/yyyyy
      $ echo /$(basename "$str")
      /yyyyy
      $ _






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 27 at 15:39

























      answered Feb 27 at 15:33









      dsstorefile1

      1,312111




      1,312111







      • 2




        Honestly, why anyone would either suggest or accept an answer involving sed (or awk, or any other similar method) instead of just using the utilities installed with the OS is beyond me.
        – Auspex
        Feb 28 at 11:48










      • @Auspex I'd guess it's simply because dirname and basename are not widely known, but sed and awk are.
        – Volker Siegel
        Feb 28 at 13:08












      • 2




        Honestly, why anyone would either suggest or accept an answer involving sed (or awk, or any other similar method) instead of just using the utilities installed with the OS is beyond me.
        – Auspex
        Feb 28 at 11:48










      • @Auspex I'd guess it's simply because dirname and basename are not widely known, but sed and awk are.
        – Volker Siegel
        Feb 28 at 13:08







      2




      2




      Honestly, why anyone would either suggest or accept an answer involving sed (or awk, or any other similar method) instead of just using the utilities installed with the OS is beyond me.
      – Auspex
      Feb 28 at 11:48




      Honestly, why anyone would either suggest or accept an answer involving sed (or awk, or any other similar method) instead of just using the utilities installed with the OS is beyond me.
      – Auspex
      Feb 28 at 11:48












      @Auspex I'd guess it's simply because dirname and basename are not widely known, but sed and awk are.
      – Volker Siegel
      Feb 28 at 13:08




      @Auspex I'd guess it's simply because dirname and basename are not widely known, but sed and awk are.
      – Volker Siegel
      Feb 28 at 13:08










      up vote
      15
      down vote













      Parameter expansion in bash



      You can use parameter expansion in bash, in this case




      • $parameter%word where word is /*


      • $parameter##word where word is */

      Examples:



      Remove the last part



      $ asdf="xxx/xxxx/xxxxx/yyy"
      $ echo $asdf%/*
      xxx/xxxx/xxxxx


      This is described in man bash:



      $parameter%word
      $parameter%%word
      Remove matching suffix pattern. The word is expanded to produce
      a pattern just as in pathname expansion. If the pattern matches
      a trailing portion of the expanded value of parameter, then the
      result of the expansion is the expanded value of parameter with
      the shortest matching pattern (the ``%'' case) or the longest
      matching pattern (the ``%%'' case) deleted. If parameter is @
      or *, the pattern removal operation is applied to each posi‐
      tional parameter in turn, and the expansion is the resultant
      list. If parameter is an array variable subscripted with @ or
      *, the pattern removal operation is applied to each member of
      the array in turn, and the expansion is the resultant list.


      Remove all except the last part



      $ asdf="xxx/xxxx/xxxxx/yyy"
      $ echo $asdf##*/
      yyy


      You can add a slash like so



      $ echo /$asdf##*/
      /yyy


      to get exactly what you wanted at one particular instance according to the edited question. But the question has been edited by several people after that and it is not easy to know what you want now.



      This is described in man bash:



      $parameter#word
      $parameter##word
      Remove matching prefix pattern. The word is expanded to produce
      a pattern just as in pathname expansion. If the pattern matches
      the beginning of the value of parameter, then the result of the
      expansion is the expanded value of parameter with the shortest
      matching pattern (the ``#'' case) or the longest matching pat‐
      tern (the ``##'' case) deleted. If parameter is @ or *, the
      pattern removal operation is applied to each positional parame‐
      ter in turn, and the expansion is the resultant list. If param‐
      eter is an array variable subscripted with @ or *, the pattern
      removal operation is applied to each member of the array in
      turn, and the expansion is the resultant list.





      share|improve this answer


























        up vote
        15
        down vote













        Parameter expansion in bash



        You can use parameter expansion in bash, in this case




        • $parameter%word where word is /*


        • $parameter##word where word is */

        Examples:



        Remove the last part



        $ asdf="xxx/xxxx/xxxxx/yyy"
        $ echo $asdf%/*
        xxx/xxxx/xxxxx


        This is described in man bash:



        $parameter%word
        $parameter%%word
        Remove matching suffix pattern. The word is expanded to produce
        a pattern just as in pathname expansion. If the pattern matches
        a trailing portion of the expanded value of parameter, then the
        result of the expansion is the expanded value of parameter with
        the shortest matching pattern (the ``%'' case) or the longest
        matching pattern (the ``%%'' case) deleted. If parameter is @
        or *, the pattern removal operation is applied to each posi‐
        tional parameter in turn, and the expansion is the resultant
        list. If parameter is an array variable subscripted with @ or
        *, the pattern removal operation is applied to each member of
        the array in turn, and the expansion is the resultant list.


        Remove all except the last part



        $ asdf="xxx/xxxx/xxxxx/yyy"
        $ echo $asdf##*/
        yyy


        You can add a slash like so



        $ echo /$asdf##*/
        /yyy


        to get exactly what you wanted at one particular instance according to the edited question. But the question has been edited by several people after that and it is not easy to know what you want now.



        This is described in man bash:



        $parameter#word
        $parameter##word
        Remove matching prefix pattern. The word is expanded to produce
        a pattern just as in pathname expansion. If the pattern matches
        the beginning of the value of parameter, then the result of the
        expansion is the expanded value of parameter with the shortest
        matching pattern (the ``#'' case) or the longest matching pat‐
        tern (the ``##'' case) deleted. If parameter is @ or *, the
        pattern removal operation is applied to each positional parame‐
        ter in turn, and the expansion is the resultant list. If param‐
        eter is an array variable subscripted with @ or *, the pattern
        removal operation is applied to each member of the array in
        turn, and the expansion is the resultant list.





        share|improve this answer
























          up vote
          15
          down vote










          up vote
          15
          down vote









          Parameter expansion in bash



          You can use parameter expansion in bash, in this case




          • $parameter%word where word is /*


          • $parameter##word where word is */

          Examples:



          Remove the last part



          $ asdf="xxx/xxxx/xxxxx/yyy"
          $ echo $asdf%/*
          xxx/xxxx/xxxxx


          This is described in man bash:



          $parameter%word
          $parameter%%word
          Remove matching suffix pattern. The word is expanded to produce
          a pattern just as in pathname expansion. If the pattern matches
          a trailing portion of the expanded value of parameter, then the
          result of the expansion is the expanded value of parameter with
          the shortest matching pattern (the ``%'' case) or the longest
          matching pattern (the ``%%'' case) deleted. If parameter is @
          or *, the pattern removal operation is applied to each posi‐
          tional parameter in turn, and the expansion is the resultant
          list. If parameter is an array variable subscripted with @ or
          *, the pattern removal operation is applied to each member of
          the array in turn, and the expansion is the resultant list.


          Remove all except the last part



          $ asdf="xxx/xxxx/xxxxx/yyy"
          $ echo $asdf##*/
          yyy


          You can add a slash like so



          $ echo /$asdf##*/
          /yyy


          to get exactly what you wanted at one particular instance according to the edited question. But the question has been edited by several people after that and it is not easy to know what you want now.



          This is described in man bash:



          $parameter#word
          $parameter##word
          Remove matching prefix pattern. The word is expanded to produce
          a pattern just as in pathname expansion. If the pattern matches
          the beginning of the value of parameter, then the result of the
          expansion is the expanded value of parameter with the shortest
          matching pattern (the ``#'' case) or the longest matching pat‐
          tern (the ``##'' case) deleted. If parameter is @ or *, the
          pattern removal operation is applied to each positional parame‐
          ter in turn, and the expansion is the resultant list. If param‐
          eter is an array variable subscripted with @ or *, the pattern
          removal operation is applied to each member of the array in
          turn, and the expansion is the resultant list.





          share|improve this answer














          Parameter expansion in bash



          You can use parameter expansion in bash, in this case




          • $parameter%word where word is /*


          • $parameter##word where word is */

          Examples:



          Remove the last part



          $ asdf="xxx/xxxx/xxxxx/yyy"
          $ echo $asdf%/*
          xxx/xxxx/xxxxx


          This is described in man bash:



          $parameter%word
          $parameter%%word
          Remove matching suffix pattern. The word is expanded to produce
          a pattern just as in pathname expansion. If the pattern matches
          a trailing portion of the expanded value of parameter, then the
          result of the expansion is the expanded value of parameter with
          the shortest matching pattern (the ``%'' case) or the longest
          matching pattern (the ``%%'' case) deleted. If parameter is @
          or *, the pattern removal operation is applied to each posi‐
          tional parameter in turn, and the expansion is the resultant
          list. If parameter is an array variable subscripted with @ or
          *, the pattern removal operation is applied to each member of
          the array in turn, and the expansion is the resultant list.


          Remove all except the last part



          $ asdf="xxx/xxxx/xxxxx/yyy"
          $ echo $asdf##*/
          yyy


          You can add a slash like so



          $ echo /$asdf##*/
          /yyy


          to get exactly what you wanted at one particular instance according to the edited question. But the question has been edited by several people after that and it is not easy to know what you want now.



          This is described in man bash:



          $parameter#word
          $parameter##word
          Remove matching prefix pattern. The word is expanded to produce
          a pattern just as in pathname expansion. If the pattern matches
          the beginning of the value of parameter, then the result of the
          expansion is the expanded value of parameter with the shortest
          matching pattern (the ``#'' case) or the longest matching pat‐
          tern (the ``##'' case) deleted. If parameter is @ or *, the
          pattern removal operation is applied to each positional parame‐
          ter in turn, and the expansion is the resultant list. If param‐
          eter is an array variable subscripted with @ or *, the pattern
          removal operation is applied to each member of the array in
          turn, and the expansion is the resultant list.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 28 at 19:28

























          answered Feb 27 at 15:33









          sudodus

          20.4k32668




          20.4k32668




















              up vote
              9
              down vote













              Another way is to use grep to only display the last slash per line and whatever follows it:



              $ grep -o '/[^/]*$' example.txt
              /yyy
              /yyyyy


              Explanation:



              -o tells grep to only show the matching part of the line instead of the whole line.



              The pattern /[^/]*$ matches a literal slash / followed by any character except for a slash [^/] any number of times * until the end of the line $.






              share|improve this answer
























                up vote
                9
                down vote













                Another way is to use grep to only display the last slash per line and whatever follows it:



                $ grep -o '/[^/]*$' example.txt
                /yyy
                /yyyyy


                Explanation:



                -o tells grep to only show the matching part of the line instead of the whole line.



                The pattern /[^/]*$ matches a literal slash / followed by any character except for a slash [^/] any number of times * until the end of the line $.






                share|improve this answer






















                  up vote
                  9
                  down vote










                  up vote
                  9
                  down vote









                  Another way is to use grep to only display the last slash per line and whatever follows it:



                  $ grep -o '/[^/]*$' example.txt
                  /yyy
                  /yyyyy


                  Explanation:



                  -o tells grep to only show the matching part of the line instead of the whole line.



                  The pattern /[^/]*$ matches a literal slash / followed by any character except for a slash [^/] any number of times * until the end of the line $.






                  share|improve this answer












                  Another way is to use grep to only display the last slash per line and whatever follows it:



                  $ grep -o '/[^/]*$' example.txt
                  /yyy
                  /yyyyy


                  Explanation:



                  -o tells grep to only show the matching part of the line instead of the whole line.



                  The pattern /[^/]*$ matches a literal slash / followed by any character except for a slash [^/] any number of times * until the end of the line $.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 27 at 15:37









                  Byte Commander

                  59.7k26159268




                  59.7k26159268




















                      up vote
                      7
                      down vote













                      Just because others have posted more “sane” answers, here’s a somewhat silly one:



                      rev | cut -d/ -f1 | rev


                      rev reverses the characters in each line, e.g. abcd/ef/g becomes g/fe/dcba. Then cut cuts out the first segment. Finally it’s reversed again.






                      share|improve this answer
















                      • 1




                        Despite the non-sanity of this answer, especially because of the shell plumbing, I like this answer :) Keep on rocking, +1
                        – Sergiy Kolodyazhnyy
                        Feb 28 at 2:32










                      • Agreed - not sane, but badass!
                        – Volker Siegel
                        Feb 28 at 12:12














                      up vote
                      7
                      down vote













                      Just because others have posted more “sane” answers, here’s a somewhat silly one:



                      rev | cut -d/ -f1 | rev


                      rev reverses the characters in each line, e.g. abcd/ef/g becomes g/fe/dcba. Then cut cuts out the first segment. Finally it’s reversed again.






                      share|improve this answer
















                      • 1




                        Despite the non-sanity of this answer, especially because of the shell plumbing, I like this answer :) Keep on rocking, +1
                        – Sergiy Kolodyazhnyy
                        Feb 28 at 2:32










                      • Agreed - not sane, but badass!
                        – Volker Siegel
                        Feb 28 at 12:12












                      up vote
                      7
                      down vote










                      up vote
                      7
                      down vote









                      Just because others have posted more “sane” answers, here’s a somewhat silly one:



                      rev | cut -d/ -f1 | rev


                      rev reverses the characters in each line, e.g. abcd/ef/g becomes g/fe/dcba. Then cut cuts out the first segment. Finally it’s reversed again.






                      share|improve this answer












                      Just because others have posted more “sane” answers, here’s a somewhat silly one:



                      rev | cut -d/ -f1 | rev


                      rev reverses the characters in each line, e.g. abcd/ef/g becomes g/fe/dcba. Then cut cuts out the first segment. Finally it’s reversed again.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 27 at 16:55









                      egmont

                      3,5691821




                      3,5691821







                      • 1




                        Despite the non-sanity of this answer, especially because of the shell plumbing, I like this answer :) Keep on rocking, +1
                        – Sergiy Kolodyazhnyy
                        Feb 28 at 2:32










                      • Agreed - not sane, but badass!
                        – Volker Siegel
                        Feb 28 at 12:12












                      • 1




                        Despite the non-sanity of this answer, especially because of the shell plumbing, I like this answer :) Keep on rocking, +1
                        – Sergiy Kolodyazhnyy
                        Feb 28 at 2:32










                      • Agreed - not sane, but badass!
                        – Volker Siegel
                        Feb 28 at 12:12







                      1




                      1




                      Despite the non-sanity of this answer, especially because of the shell plumbing, I like this answer :) Keep on rocking, +1
                      – Sergiy Kolodyazhnyy
                      Feb 28 at 2:32




                      Despite the non-sanity of this answer, especially because of the shell plumbing, I like this answer :) Keep on rocking, +1
                      – Sergiy Kolodyazhnyy
                      Feb 28 at 2:32












                      Agreed - not sane, but badass!
                      – Volker Siegel
                      Feb 28 at 12:12




                      Agreed - not sane, but badass!
                      – Volker Siegel
                      Feb 28 at 12:12










                      up vote
                      3
                      down vote













                      Classic solution with awk, that treats / as field separator for both input and output and sets the last field to empty string ( which really is "dereferencing" of the number of fields NF variable ):



                      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";$NF="";print $0'
                      xxx/xxxx/xxxxx/


                      Shorter, as fedorqui pointed out in the comments:



                      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";NF--'
                      xxx/xxxx/xxxxx/


                      Variation on that would be to put path into awk's execution environment, which will save up on plumbing but makes awk portion more verbose:



                      mypath="xxx/xxxx/xxxxx/yyy" awk 'BEGINOFS=FS="/"; sub(//([^/]*)$/,"",ENVIRON["mypath"]);print(ENVIRON["mypath"]) ;'





                      share|improve this answer


















                      • 1




                        Too verbose, why not just NF--? This way you avoud the print bla bla.
                        – fedorqui
                        Feb 28 at 9:35















                      up vote
                      3
                      down vote













                      Classic solution with awk, that treats / as field separator for both input and output and sets the last field to empty string ( which really is "dereferencing" of the number of fields NF variable ):



                      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";$NF="";print $0'
                      xxx/xxxx/xxxxx/


                      Shorter, as fedorqui pointed out in the comments:



                      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";NF--'
                      xxx/xxxx/xxxxx/


                      Variation on that would be to put path into awk's execution environment, which will save up on plumbing but makes awk portion more verbose:



                      mypath="xxx/xxxx/xxxxx/yyy" awk 'BEGINOFS=FS="/"; sub(//([^/]*)$/,"",ENVIRON["mypath"]);print(ENVIRON["mypath"]) ;'





                      share|improve this answer


















                      • 1




                        Too verbose, why not just NF--? This way you avoud the print bla bla.
                        – fedorqui
                        Feb 28 at 9:35













                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      Classic solution with awk, that treats / as field separator for both input and output and sets the last field to empty string ( which really is "dereferencing" of the number of fields NF variable ):



                      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";$NF="";print $0'
                      xxx/xxxx/xxxxx/


                      Shorter, as fedorqui pointed out in the comments:



                      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";NF--'
                      xxx/xxxx/xxxxx/


                      Variation on that would be to put path into awk's execution environment, which will save up on plumbing but makes awk portion more verbose:



                      mypath="xxx/xxxx/xxxxx/yyy" awk 'BEGINOFS=FS="/"; sub(//([^/]*)$/,"",ENVIRON["mypath"]);print(ENVIRON["mypath"]) ;'





                      share|improve this answer














                      Classic solution with awk, that treats / as field separator for both input and output and sets the last field to empty string ( which really is "dereferencing" of the number of fields NF variable ):



                      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";$NF="";print $0'
                      xxx/xxxx/xxxxx/


                      Shorter, as fedorqui pointed out in the comments:



                      $ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGINOFS=FS="/";NF--'
                      xxx/xxxx/xxxxx/


                      Variation on that would be to put path into awk's execution environment, which will save up on plumbing but makes awk portion more verbose:



                      mypath="xxx/xxxx/xxxxx/yyy" awk 'BEGINOFS=FS="/"; sub(//([^/]*)$/,"",ENVIRON["mypath"]);print(ENVIRON["mypath"]) ;'






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 28 at 9:54

























                      answered Feb 28 at 2:43









                      Sergiy Kolodyazhnyy

                      65.7k9134287




                      65.7k9134287







                      • 1




                        Too verbose, why not just NF--? This way you avoud the print bla bla.
                        – fedorqui
                        Feb 28 at 9:35













                      • 1




                        Too verbose, why not just NF--? This way you avoud the print bla bla.
                        – fedorqui
                        Feb 28 at 9:35








                      1




                      1




                      Too verbose, why not just NF--? This way you avoud the print bla bla.
                      – fedorqui
                      Feb 28 at 9:35





                      Too verbose, why not just NF--? This way you avoud the print bla bla.
                      – fedorqui
                      Feb 28 at 9:35











                      up vote
                      2
                      down vote













                      Adding to egmont's answer because the question has been edited...



                      Use --complement if you want to remove the first field with -f1.



                      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1 --complement|rev
                      xxxx/x/xx/xx/xxxx/x

                      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1|rev
                      yyyyy


                      Also, the question isn't quite clear about what should happen with inputs not containing any slashes.
                      xxxx => xxxx
                      or
                      xxxx => nothing






                      share|improve this answer






















                      • You can suggest an edit to it: askubuntu.com/posts/1010356/edit
                        – muru
                        Feb 28 at 14:08










                      • @muru OP is 1 rep user. Isn't editing privilege is granted at 2k ? askubuntu.com/help/privileges/edit
                        – Sergiy Kolodyazhnyy
                        Feb 28 at 18:47










                      • @SergiyKolodyazhnyy anybody can suggest edits.
                        – muru
                        Feb 28 at 21:26










                      • +1 - I had not noticed the --complement option before and the man page is circular. Complement means complement. I found a nice cut tutorial which covers it at yourownlinux.com/2015/05/… It's like -v in grep. Give me everything except what was matched.
                        – Joe
                        Feb 28 at 23:59







                      • 1




                        -f2- is a simpler form for -f1 --complement.
                        – egmont
                        Mar 1 at 0:04














                      up vote
                      2
                      down vote













                      Adding to egmont's answer because the question has been edited...



                      Use --complement if you want to remove the first field with -f1.



                      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1 --complement|rev
                      xxxx/x/xx/xx/xxxx/x

                      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1|rev
                      yyyyy


                      Also, the question isn't quite clear about what should happen with inputs not containing any slashes.
                      xxxx => xxxx
                      or
                      xxxx => nothing






                      share|improve this answer






















                      • You can suggest an edit to it: askubuntu.com/posts/1010356/edit
                        – muru
                        Feb 28 at 14:08










                      • @muru OP is 1 rep user. Isn't editing privilege is granted at 2k ? askubuntu.com/help/privileges/edit
                        – Sergiy Kolodyazhnyy
                        Feb 28 at 18:47










                      • @SergiyKolodyazhnyy anybody can suggest edits.
                        – muru
                        Feb 28 at 21:26










                      • +1 - I had not noticed the --complement option before and the man page is circular. Complement means complement. I found a nice cut tutorial which covers it at yourownlinux.com/2015/05/… It's like -v in grep. Give me everything except what was matched.
                        – Joe
                        Feb 28 at 23:59







                      • 1




                        -f2- is a simpler form for -f1 --complement.
                        – egmont
                        Mar 1 at 0:04












                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      Adding to egmont's answer because the question has been edited...



                      Use --complement if you want to remove the first field with -f1.



                      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1 --complement|rev
                      xxxx/x/xx/xx/xxxx/x

                      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1|rev
                      yyyyy


                      Also, the question isn't quite clear about what should happen with inputs not containing any slashes.
                      xxxx => xxxx
                      or
                      xxxx => nothing






                      share|improve this answer














                      Adding to egmont's answer because the question has been edited...



                      Use --complement if you want to remove the first field with -f1.



                      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1 --complement|rev
                      xxxx/x/xx/xx/xxxx/x

                      echo "xxxx/x/xx/xx/xxxx/x/yyyyy"|rev|cut -d/ -f1|rev
                      yyyyy


                      Also, the question isn't quite clear about what should happen with inputs not containing any slashes.
                      xxxx => xxxx
                      or
                      xxxx => nothing







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 28 at 14:09

























                      answered Feb 28 at 14:07









                      Ph3n0x

                      212




                      212











                      • You can suggest an edit to it: askubuntu.com/posts/1010356/edit
                        – muru
                        Feb 28 at 14:08










                      • @muru OP is 1 rep user. Isn't editing privilege is granted at 2k ? askubuntu.com/help/privileges/edit
                        – Sergiy Kolodyazhnyy
                        Feb 28 at 18:47










                      • @SergiyKolodyazhnyy anybody can suggest edits.
                        – muru
                        Feb 28 at 21:26










                      • +1 - I had not noticed the --complement option before and the man page is circular. Complement means complement. I found a nice cut tutorial which covers it at yourownlinux.com/2015/05/… It's like -v in grep. Give me everything except what was matched.
                        – Joe
                        Feb 28 at 23:59







                      • 1




                        -f2- is a simpler form for -f1 --complement.
                        – egmont
                        Mar 1 at 0:04
















                      • You can suggest an edit to it: askubuntu.com/posts/1010356/edit
                        – muru
                        Feb 28 at 14:08










                      • @muru OP is 1 rep user. Isn't editing privilege is granted at 2k ? askubuntu.com/help/privileges/edit
                        – Sergiy Kolodyazhnyy
                        Feb 28 at 18:47










                      • @SergiyKolodyazhnyy anybody can suggest edits.
                        – muru
                        Feb 28 at 21:26










                      • +1 - I had not noticed the --complement option before and the man page is circular. Complement means complement. I found a nice cut tutorial which covers it at yourownlinux.com/2015/05/… It's like -v in grep. Give me everything except what was matched.
                        – Joe
                        Feb 28 at 23:59







                      • 1




                        -f2- is a simpler form for -f1 --complement.
                        – egmont
                        Mar 1 at 0:04















                      You can suggest an edit to it: askubuntu.com/posts/1010356/edit
                      – muru
                      Feb 28 at 14:08




                      You can suggest an edit to it: askubuntu.com/posts/1010356/edit
                      – muru
                      Feb 28 at 14:08












                      @muru OP is 1 rep user. Isn't editing privilege is granted at 2k ? askubuntu.com/help/privileges/edit
                      – Sergiy Kolodyazhnyy
                      Feb 28 at 18:47




                      @muru OP is 1 rep user. Isn't editing privilege is granted at 2k ? askubuntu.com/help/privileges/edit
                      – Sergiy Kolodyazhnyy
                      Feb 28 at 18:47












                      @SergiyKolodyazhnyy anybody can suggest edits.
                      – muru
                      Feb 28 at 21:26




                      @SergiyKolodyazhnyy anybody can suggest edits.
                      – muru
                      Feb 28 at 21:26












                      +1 - I had not noticed the --complement option before and the man page is circular. Complement means complement. I found a nice cut tutorial which covers it at yourownlinux.com/2015/05/… It's like -v in grep. Give me everything except what was matched.
                      – Joe
                      Feb 28 at 23:59





                      +1 - I had not noticed the --complement option before and the man page is circular. Complement means complement. I found a nice cut tutorial which covers it at yourownlinux.com/2015/05/… It's like -v in grep. Give me everything except what was matched.
                      – Joe
                      Feb 28 at 23:59





                      1




                      1




                      -f2- is a simpler form for -f1 --complement.
                      – egmont
                      Mar 1 at 0:04




                      -f2- is a simpler form for -f1 --complement.
                      – egmont
                      Mar 1 at 0:04

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1010310%2fcutting-all-the-characters-after-the-last%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      pylint3 and pip3 broken

                      Missing snmpget and snmpwalk

                      How to enroll fingerprints to Ubuntu 17.10 with VFS491