Glam Prestige Journal

Bright entertainment trends with youth appeal.

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
5

7 Answers

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
2

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
$ _
3

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

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.

3

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 $.

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 'BEGIN{OFS=FS="/"};{$NF="";print $0}'
xxx/xxxx/xxxxx/

Shorter, as fedorqui pointed out in the comments:

$ echo "xxx/xxxx/xxxxx/yyy"|awk 'BEGIN{OFS=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 'BEGIN{OFS=FS="/"; sub(/\/([^\/]*)$/,"",ENVIRON["mypath"]);print(ENVIRON["mypath"]) };'
1

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

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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