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)
小智 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)
sud*_*dus 18
bashbash在这种情况下,您可以在 中使用参数扩展
${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切出第一段。最后又反过来了。