Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中将子字符串解析为shell变量_Bash_Shell - Fatal编程技术网

在bash中将子字符串解析为shell变量

在bash中将子字符串解析为shell变量,bash,shell,Bash,Shell,我们得到了以下输入: [18:51:10] [Server thread/INFO]: Tester121 entered area ~ Wilderness - (PvP) ~ time="18:51:10" player="Tester121" area="Wilderness" 我们想要解释输入并定义不同的变量 [$time] [Server thread/INFO]: $player entered area ~ $area - (PvP) ~ 结果应为: [18:51:10] [

我们得到了以下输入:

[18:51:10] [Server thread/INFO]: Tester121 entered area ~ Wilderness - (PvP) ~
time="18:51:10"
player="Tester121"
area="Wilderness"
我们想要解释输入并定义不同的变量

[$time] [Server thread/INFO]: $player entered area ~ $area - (PvP) ~
结果应为:

[18:51:10] [Server thread/INFO]: Tester121 entered area ~ Wilderness - (PvP) ~
time="18:51:10"
player="Tester121"
area="Wilderness"

我是stackoverflow新手,请随时发表评论,以便提高我的提问技巧。

中的
=~
操作符以POSIX ERE格式计算正则表达式,并将生成的匹配组分配给数组
BASH\u REMATCH

re='^\[([[:digit:]:]+)\] \[Server thread/INFO\]: ([^[:space:]]+) entered area ~ ([^[:space:]]+) - [(]PvP[)] ~'
line='[18:51:10] [Server thread/INFO]: Tester121 entered area ~ Wilderness - (PvP) ~'

if [[ $line =~ $re ]]; then
  time=${BASH_REMATCH[1]}
  player=${BASH_REMATCH[2]}
  area=${BASH_REMATCH[3]}
fi

这实际上是构建库的一类任务。可以认为是重复的,但尽管答案很好,但这是一个非常糟糕的问题……虽然是一个更好的问题,但有几个答案做出了不适用于本案例的假设。以使现有答案无效的方式编辑问题在这里并不符合犹太教义。这种做法产生了一个所谓的“变色龙问题”,他们很抱歉。但这很好,如果我将输入定义为$input,如何测试它?谢谢你的快速反应伙伴。没有看到你编辑的答案,我会测试:)我在问题上加了点东西。你能再帮忙吗?我对regex还不熟悉,并尝试学习:)这应该是一个单独的问题。我建议将您尝试过的内容(您自己尝试过一个已编辑的正则表达式)包含在其中,这样回答它的人就可以描述它是如何失败的……不过,一旦您提出了单独的问题,请在这里链接到它。(不过,我可能要去吃午饭了)。