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

时间:2022-01-12 20:19:38

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中这样做?

4 个解决方案

#1


15  

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"

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

#2


21  

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 first occurrence, instead of the last, just use one #:

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

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

#3


7  

Try this:

尝试这个:

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

The rev reverses the output. The -d specifies the delimiter, breaking everything up into fields. The -f specifies 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.

转速反转输出。 -d指定分隔符,将所有内容分解为字段。 -f指定要使用的字段。我们可以选择f1,因为我们反转了数据。我们不需要知道总共有多少个字段。我们只需知道第一个。最后,我们再次对其进行反转,以正确的顺序将其恢复。

#4


4  

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年,但是我的一位牛犊今天指出我正在处理的这个页面,所以我想我会把它放在这里以防其他人也在看。)

#1


15  

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"

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

#2


21  

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 first occurrence, instead of the last, just use one #:

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

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

#3


7  

Try this:

尝试这个:

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

The rev reverses the output. The -d specifies the delimiter, breaking everything up into fields. The -f specifies 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.

转速反转输出。 -d指定分隔符,将所有内容分解为字段。 -f指定要使用的字段。我们可以选择f1,因为我们反转了数据。我们不需要知道总共有多少个字段。我们只需知道第一个。最后,我们再次对其进行反转,以正确的顺序将其恢复。

#4


4  

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年,但是我的一位牛犊今天指出我正在处理的这个页面,所以我想我会把它放在这里以防其他人也在看。)