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
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
#
instead of /
. Also you can use the option -r
if you don't want to escape ` \ ` each special character: sed -r 's#(^.*)/.*$#\1#'
.
bash
You can use parameter expansion in bash
, in this case
${parameter%word}
where word
is /*
${parameter##word}
where word
is */
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.
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
$ _
dirname
and basename
are not widely known, but sed
and awk
are.
Feb 28, 2018 at 13:08
cat text | basename
won't work. So a proper solution to OP's question using dirname
and basename
would involve xargs
etc.
Just because others have posted more “sane” answers, here’s a somewhat silly one:
rev | cut -d/ -f2- | rev
rev
reverses the characters in each line, e.g. abcd/ef/g
becomes g/fe/dcba
. Then cut
chops off the first segment. Finally it’s reversed again.
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"]) };'
NF--
? This way you avoud the print
bla bla.
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*
--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.
basename
anddirname
do this already./
" 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.