删除最后一个 / 之后的所有字符

Jos*_*muk 22 command-line text-processing

如何剪切最后一个“/”之后的所有字符?

本文

xxxx/x/xx/xx/xxxx/x/yyyyy
xxx/xxxx/xxxxx/yyy
Run Code Online (Sandbox Code Playgroud)

应该回来

xxxx/x/xx/xx/xxxx/x
xxx/xxxx/xxxxx
Run Code Online (Sandbox Code Playgroud)

Fél*_*ien 26

如果你想得到“切割的部分”

yyy
yyyyy
Run Code Online (Sandbox Code Playgroud)

您可以使用

sed 's|.*/||' 
Run Code Online (Sandbox Code Playgroud)

例如。

echo "xxx/xxxx/xxxxx/yyy" | sed 's|.*/||'
echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|.*\/||'
Run Code Online (Sandbox Code Playgroud)

输出

yyy
yyyyy
Run Code Online (Sandbox Code Playgroud)

(注意:这使用了 sed 在 s 命令中使用 / 以外的分隔符的能力,在本例中为 |)


如果你想获得字符串的开头:

xxx/xxxx/xxxxx
xxxx/x/xx/xx/xxxx/x
Run Code Online (Sandbox Code Playgroud)

您可以使用

sed 's|\(.*\)/.*|\1|'
Run Code Online (Sandbox Code Playgroud)

例如。

echo "xxx/xxxx/xxxxx/yyy" | sed 's|\(.*\)/.*|\1|'
echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|\(.*\)/.*|\1|'
Run Code Online (Sandbox Code Playgroud)

输出

xxx/xxxx/xxxxx
xxxx/x/xx/xx/xxxx/x
Run Code Online (Sandbox Code Playgroud)

  • 您好,在这种情况下,您可以更改分隔符,例如用`#` 代替`/`。如果您不想对每个特殊字符进行转义,也可以使用选项 `-r`:`sed -r 's#(^.*)/.*$#\1#'`。 (6认同)

小智 21

如果你正在切割弦的两端,dirname可能适合该法案:

$ dirname xxxx/x/xx/xx/xxxx/x/yyyyy
xxxx/x/xx/xx/xxxx/x
$ _
Run Code Online (Sandbox Code Playgroud)

如果您试图隔离字符串的最后一部分,请使用echo /$(basename "$str").

$ str=xxxx/x/xx/xx/xxxx/x/yyyyy
$ echo /$(basename "$str")
/yyyyy
$ _
Run Code Online (Sandbox Code Playgroud)

  • 老实说,为什么有人会建议_或接受_涉及 sed(或 awk,或任何其他类似方法)的答案,而不是仅使用随操作系统安装的实用程序,这超出了我的理解。 (3认同)

sud*_*dus 18

参数扩展 bash

bash在这种情况下,您可以在 中使用参数扩展

  • ${parameter%word}这里word/*
  • ${parameter##word}这里word*/

例子:

删除最后一部分

$ asdf="xxx/xxxx/xxxxx/yyy"
$ echo ${asdf%/*}
xxx/xxxx/xxxxx
Run Code Online (Sandbox Code Playgroud)

这在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.
Run Code Online (Sandbox Code Playgroud)

删除除最后一部分之外的所有内容

$ asdf="xxx/xxxx/xxxxx/yyy"
$ echo ${asdf##*/}
yyy
Run Code Online (Sandbox Code Playgroud)

您可以像这样添加斜杠

$ echo /${asdf##*/}
/yyy
Run Code Online (Sandbox Code Playgroud)

根据编辑过的问题,在某个特定情况下准确获得您想要的内容。但是问题在那之后已经被几个人编辑过,现在很难知道你想要什么。

这在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.
Run Code Online (Sandbox Code Playgroud)


Byt*_*der 11

另一种方法是使用grep只显示每行的最后一个斜杠及其后面的内容:

$ grep -o '/[^/]*$' example.txt
/yyy
/yyyyy
Run Code Online (Sandbox Code Playgroud)

解释:

-o告诉grep只显示该行的匹配部分而不是整行。

该模式/[^/]*$匹配文字斜杠/后跟任何字符,斜杠除外,直到行尾[^/]任意次数。*$


egm*_*ont 11

仅仅因为其他人发布了更多“理智”的答案,这里有一个有点愚蠢的答案:

rev | cut -d/ -f1 | rev
Run Code Online (Sandbox Code Playgroud)

rev反转每一行中的字符,例如abcd/ef/g变成g/fe/dcba. 然后cut切出第一段。最后又反过来了。

  • 尽管这个答案不理智,特别是因为外壳管道,我喜欢这个答案:) 继续摇摆,+1 (2认同)