string 如何在bash中提取字符串的最后一部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12426659/
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
How extract last part of string in bash?
提问by user710818
I have variable:
我有变量:
A="Some variable has value abc.123"
I need to extract this value i.e abc.123
. Is this possible in bash?
我需要提取这个值,即abc.123
。这在 bash 中可能吗?
采纳答案by unwind
How do you know where the value begins? If it's always the 5th and 6th words, you could use e.g.:
你怎么知道值从哪里开始?如果它总是第 5 个和第 6 个词,你可以使用例如:
B=$(echo $A | cut -d ' ' -f 5-)
This uses the cut
command to slice out part of the line, using a simple space as the word delimiter.
这使用cut
命令切出行的一部分,使用一个简单的空格作为单词分隔符。
回答by gammay
Simplest is
最简单的是
echo $A | awk '{print $NF}'
Edit: explanation of how this works...
编辑:解释这是如何工作的......
awk
breaks the input into different fields, using whitespace as the separator by default. Hardcoding 5
in place of NF
prints out the 5th field in the input:
awk
将输入分成不同的字段,默认情况下使用空格作为分隔符。硬编码5
代替NF
打印出输入中的第 5 个字段:
echo $A | awk '{print }'
NF
is a built-in awk
variable that gives the total number of fields in the current record. The following returns the number 5 because there are 5 fields in the string "Some variable has value abc.123"
:
NF
是一个内置awk
变量,用于给出当前记录中的字段总数。以下返回数字 5,因为字符串中有 5 个字段"Some variable has value abc.123"
:
echo $A | awk '{print NF}'
Combining $
with NF
outputs the last field in the string, no matter how many fields your string contains.
$
与NF
输出字符串中的最后一个字段相结合,无论您的字符串包含多少个字段。
回答by ruakh
Yes; this:
是的; 这个:
A="Some variable has value abc.123"
echo "${A##* }"
will print this:
将打印这个:
abc.123
(The ${parameter##word}
notation is explained in §3.5.3 "Shell Parameter Expansion" of the Bash Reference Manual.)
(该符号在Bash 参考手册的第 3.5.3 节“外壳参数扩展”中进行了解释。)${parameter##word}
回答by koola
Some examples using parameter expansion
一些使用参数扩展的例子
A="Some variable has value abc.123"
echo "${A##* }"
abc.123
Longest match on " " space
" " 空格上的最长匹配
echo "${A% *}"
Some variable has value
Longest match on . dot
上最长的比赛。点
echo "${A%.*}"
Some variable has value abc
Shortest match on " " space
" " 空间的最短匹配
echo "${A%% *}"
some
Read more Shell-Parameter-Expansion
回答by mwfearnley
The documentationis a bit painful to read, so I've summarised it in a simpler way.
该文档是有点痛苦的阅读,所以我在一个简单的方法概括它。
Note that the '*
' needs to swap places with the '' depending on whether you use
#
or %
. (The *
is just a wildcard, so you may need to take off your "regex hat" while reading.)
请注意, ' *
' 需要与 ' '交换位置,具体取决于您使用
#
或%
。(这*
只是一个通配符,因此您可能需要在阅读时摘下“正则表达式帽子”。)
${A%% *}
- remove longest trailing*
(keep only the first word)${A% *}
- remove shortest trailing*
(keep all but the last word)${A##* }
- remove longest leading*
(keep only the last word)${A#* }
- remove shortest leading*
(keep all but the first word)
${A%% *}
- 删除最长的尾随*
(只保留第一个单词)${A% *}
- 删除最短的尾随*
(保留最后一个字以外的所有内容)${A##* }
- 删除最长的前导*
(只保留最后一个单词)${A#* }
- 删除最短的前导*
(保留第一个单词以外的所有单词)
Of course a "word" here may contain any character that isn't a literal space.
当然,这里的“单词”可能包含任何不是文字空间的字符。