bash 如何在shell中拆分字符串并获取最后一个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3162385/
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-09-09 19:25:21  来源:igfitidea点击:

How to split a string in shell and get the last field

bashsplittokenizecut

提问by cd1

Suppose I have the string 1:2:3:4:5and I want to get its last field (5in this case). How do I do that using Bash? I tried cut, but I don't know how to specify the last field with -f.

假设我有字符串1:2:3:4:5并且我想获取它的最后一个字段(5在这种情况下)。我如何使用 Bash 做到这一点?我试过了cut,但我不知道如何用-f.

回答by Stephen

You can use string operators:

您可以使用字符串运算符

$ foo=1:2:3:4:5
$ echo ${foo##*:}
5

This trims everything from the front until a ':', greedily.

这会贪婪地修剪从前面到“:”的所有内容。

${foo  <-- from variable foo
  ##   <-- greedy front trim
  *    <-- matches anything
  :    <-- until the last ':'
 }

回答by a3nm

Another way is to reverse before and after cut:

另一种方法是前后颠倒cut

$ echo ab:cd:ef | rev | cut -d: -f1 | rev
ef

This makes it very easy to get the last but one field, or any range of fields numbered from the end.

这使得获得最后一个字段或从末尾编号的任何字段范围变得非常容易。

回答by William Pursell

It's difficult to get the last field using cut, but here are some solutions in awk and perl

使用 cut 很难得到最后一个字段,但这里有一些 awk 和 perl 的解决方案

echo 1:2:3:4:5 | awk -F: '{print $NF}'
echo 1:2:3:4:5 | perl -F: -wane 'print $F[-1]'

回答by Nicholas M T Elliott

Assuming fairly simple usage (no escaping of the delimiter, for example), you can use grep:

假设使用相当简单(例如,没有转义分隔符),您可以使用 grep:

$ echo "1:2:3:4:5" | grep -oE "[^:]+$"
5

Breakdown - find all the characters not the delimiter ([^:]) at the end of the line ($). -o only prints the matching part.

细分 - 在行 ($) 的末尾找到除分隔符 ([^:]) 之外的所有字符。-o 只打印匹配的部分。

回答by Paused until further notice.

One way:

单程:

var1="1:2:3:4:5"
var2=${var1##*:}

Another, using an array:

另一个,使用数组:

var1="1:2:3:4:5"
saveIFS=$IFS
IFS=":"
var2=($var1)
IFS=$saveIFS
var2=${var2[@]: -1}

Yet another with an array:

另一个带有数组的:

var1="1:2:3:4:5"
saveIFS=$IFS
IFS=":"
var2=($var1)
IFS=$saveIFS
count=${#var2[@]}
var2=${var2[$count-1]}

Using Bash (version >= 3.2) regular expressions:

使用 Bash(版本 >= 3.2)正则表达式:

var1="1:2:3:4:5"
[[ $var1 =~ :([^:]*)$ ]]
var2=${BASH_REMATCH[1]}

回答by user3133260

$ echo "a b c d e" | tr ' ' '\n' | tail -1
e

Simply translate the delimiter into a newline and choose the last entry with tail -1.

只需将分隔符转换为换行符并使用 选择最后一个条目tail -1

回答by Rafael

Using sed:

使用sed

$ echo '1:2:3:4:5' | sed 's/.*://' # => 5

$ echo '' | sed 's/.*://' # => (empty)

$ echo ':' | sed 's/.*://' # => (empty)
$ echo ':b' | sed 's/.*://' # => b
$ echo '::c' | sed 's/.*://' # => c

$ echo 'a' | sed 's/.*://' # => a
$ echo 'a:' | sed 's/.*://' # => (empty)
$ echo 'a:b' | sed 's/.*://' # => b
$ echo 'a::c' | sed 's/.*://' # => c

回答by 021

There are many good answers here, but still I want to share this one using basename:

这里有很多很好的答案,但我仍然想使用basename分享这个:

 basename $(echo "a:b:c:d:e" | tr ':' '/')

However it will fail if there are already some '/' in your string. If slash / is your delimiter then you just have to (and should) use basename.

但是,如果您的字符串中已经有一些 '/' ,它将失败。如果斜线 / 是您的分隔符,那么您只需要(并且应该)使用 basename。

It's not the best answer but it just shows how you can be creative using bash commands.

这不是最好的答案,但它只是展示了如何使用 bash 命令发挥创意。

回答by Ab Irato

If your last field is a single character, you could do this:

如果您的最后一个字段是单个字符,您可以这样做:

a="1:2:3:4:5"

echo ${a: -1}
echo ${a:(-1)}

Check string manipulation in bash.

检查bash 中的字符串操作

回答by ghostdog74

Using Bash.

使用 Bash。

$ var1="1:2:3:4:0"
$ IFS=":"
$ set -- $var1
$ eval echo  $${#}
0