Linux 从字符串中获取子字符串的最后一个索引之后的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15548277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:30:10  来源:igfitidea点击:

Get the characters after the last index of a substring from a string

linuxbashunix

提问by Patartics Milán

I have a string which is an output of another command. I only need the end of this string to display. The separator string is "." (dot and space), and I need the string after the last index of ".".

我有一个字符串,它是另一个命令的输出。我只需要显示这个字符串的结尾。分隔符字符串是“ .”(点和空格),我需要“ .”的最后一个索引之后的字符串。

How can I do this in Bash?

我怎样才能在 Bash 中做到这一点?

采纳答案by Kent

try this:

尝试这个:

your cmd...|sed 's/.*\. //'

this works no matter how many "dot" or "dot and space" do you have in your input. it takes the string after the last"dot and space"

无论您的输入中有多少“点”或“点和空间”,这都有效。它需要最后一个“点和空格”之后的字符串

回答by Paul Calabro

Try this:

尝试这个:

echo "This is a sentence. This is another sentence" | rev | cut -d "." -f1 | rev

The revreverses the output. The -dspecifies the delimiter, breaking everything up into fields. The -fspecifies the fields you want to use. We can select f1, because we reversed the data. We don't need to know how many fields there are in total. We just need to know the first. At the end, we reverse it again, to put it back in the right order.

rev反转输出。在-d指定的分隔符,打破一切成田。在 -f指定的字段要使用。我们可以选择f1,因为我们颠倒了数据。我们不需要知道总共有多少个字段。我们只需要知道第一个。最后,我们再次反转它,将它放回正确的顺序。

回答by chepner

If the string is in a variable:

如果字符串在变量中:

$ foo="header. stuff. more stuff"
$ echo "${foo##*. }"
more stuff

If there are multiple instances of ". " (as in my example) and you want everything after the firstoccurrence, instead of the last, just use one #:

如果有多个“.”实例(如我的示例)并且您想要第一次出现后的所有内容,而不是最后一次,只需使用一个#

$ echo "${foo#*. }"
stuff. more stuff

回答by Petro

Awk is elegant weapon...for a more civilized age:

Awk 是优雅的武器……对于更文明的时代:

[cpetro01@h ~]$ echo "this. is. my. string. of. some. arbitrary. length" | awk -F'. ' ' { print $NF } '
length
[cpetro01@h ~]$ echo "this. is. my. string. of. some" | awk -F'. ' ' { print $NF } '   
some

In this case NF is the awk variable for "Number of fields" and this construct says "print the entry in highest number of fields found" so if the size of your input changes from one line to the next you're still going to get the last one.

在这种情况下,NF 是“字段数”的 awk 变量,并且该构造表示“在找到的最多字段数中打印条目”,因此如果输入的大小从一行更改为下一行,您仍然会得到最后一个。

You can also do math:

你也可以做数学:

[cpetro01@h~]$ echo "this. is. my. string. of. some. arbitrary. length" | awk -F'. ' ' { print $(NF-2) } '
some
[cpetro01@h~]$ echo "this. is. my. string. of. some. arbitrary. length" | awk -F'. ' ' { print $(NF-3) } '
of
[cpetro01@h~]$

(Yes, this is 3 years late for the OP, but one of my cow-orkers pointed me to this page today for something we were working on, so I thought I'd drop this here in case others are looking too.)

(是的,这对于 OP 来说已经晚了 3 年,但是我的一位同事今天将我指向了这个页面,因为我们正在做一些事情,所以我想我会把它放在这里,以防其他人也在寻找。)