从空间标记处输出阵列时,bash弦被切断
因此,我正在浏览日志文件,并从上述日志文件中提取信息,例如设备名称,位置和日志时间。它看起来像以下内容:
edge2.mco1-9 Jun 11:32:24 GMT %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success
我遇到一个问题,当我运行以下内容时:
for y in ${DeviceName[@]}
do
echo "Device: $y"
counter=1
for z in ${LocationName[@]}
do
testing=($(grep "$y\.$z\-.*\%" hacker.txt | cut -f2 -d "-" | cut -f1 -d% | head -n 1))
if [[ ${testing[$counter]} =~ [^0-9] ]]
then
echo $z
echo $testing
fi
done
done
日号是唯一输出的部分。 DeviceName是设备名称的数组。位置名称是位置名称的数组。复杂的部分是由于我想在GREP中出现某些内容时才想输出位置名称,因为我不想要一长串没有时间的位置列表。我确实尝试了保存到.txt文件的实验,该文件确实可以完成正确的时间,但是由于空格,似乎要输出正确的时间更麻烦。使用示例,
9 Jun 11:32:24 GMT
应该是输出。有什么办法解决这个问题吗? $ testing
将尝试做$ {testing [$ counter]}
,$($ {testing [$ counter]})
> ,其他方法无效,给我错误。错误是JUN
和./ Finder:Line 113:JUN:找不到命令
$ testing至少给我的日常数字。显然,空间存在问题,但我不知道如何解决此问题。
So I am going through a log file and extracting information from said log file such as device name, location, and time of log. It looks something like the following:
edge2.mco1-9 Jun 11:32:24 GMT %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success
I am coming across an issue where when I run the following:
for y in ${DeviceName[@]}
do
echo "Device: $y"
counter=1
for z in ${LocationName[@]}
do
testing=($(grep "$y\.$z\-.*\%" hacker.txt | cut -f2 -d "-" | cut -f1 -d% | head -n 1))
if [[ ${testing[$counter]} =~ [^0-9] ]]
then
echo $z
echo $testing
fi
done
done
the day number is the only part outputted. DeviceName is an array of device names. LocationName is an array of location names. The complicated part was due to me wanting to only output the location name if it had something coming from the grep since I did not want a long list of locations that had no time attached to it. I did experiment with saving to a .txt file, which did do the correct time, but it seems outputting the correct time is more troublesome due to spaces. Using the example,
9 Jun 11:32:24 GMT
should be the output. Is there any way to get around this?
The $testing
is kept as attempts to do ${testing[$counter]}
, $(${testing[$counter]})
, and other methods did not work giving me errors. Errors are Jun
and ./finder: line 113: Jun: command not found
respectively $testing at least gives me the day number. Obviously there is an issue with spaces but I don't know how to fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置:
GREP
命令:将其加载到
testing []
数组中:运行测试:
到目前为止还不错。
OP表示所需的输出应该看起来像:
但是当前
echo
命令有一个问题:$ testing
是一个数组;没有索引bash
处理$ testing
与$ {testing [0]}
相同,在这种情况下,输出9
(请参阅类型-P Testing的输出
- 上面);要输出数组的整个内容,op可以使用:net结果:op应该能够将
echo $ testing
更改为echo“ $ {testing [@]}”
to获取所需的输出。OP提到了代码复杂性(加载
testing []
数组,比较测试[1]
???)仅是由于grep 找到比赛。
我们可以通过以下方式替换循环的
的胆量来简化代码:
如果目的是仅打印
$ {z}
当grep 找到匹配项(即,OP不需要打印
中唯一的命令>循环是:$ {dtStamp}
),我们可以将代码简化为z演示:
注释:
counter = 1
,应考虑切割n-past-n-pasts n-past stast n-pasts n-pasts n-past and past and pasts n-past and past and past n-past n-post例如,#!/bin/bash
)进入 shellCheck.net ;这将报告语法问题,并提出一些建议:“最佳实践”Setup:
The
grep
command:Loading this into the
testing[]
array:Running the test:
So far so good.
OP has stated the desired output should look like:
But the current
echo
command has an issue:$testing
is an array; without an indexbash
treats$testing
the same as${testing[0]}
which in this case outputs9
(see the output fromtypeset -p testing
- above); to output the entire contents of the array OP can use:Net result: OP should be able to change
echo $testing
toecho "${testing[@]}"
to obtain the desired output.OP has mentioned the code complication (loading
testing[]
array, comparingtesting[1]
???) is due to only printing output ifgrep
finds a match.We can streamline the code a bit by replacing the guts of the
for z
loop with:If the objective is to only print
${z}
whengrep
finds a match (ie, OP doesn't need to print${dtstamp}
) we can streamline the code a bit more to where the only command in thefor z
loop is:Demo:
NOTES:
counter=1
from the current code#!/bin/bash
) into shellcheck.net; this will report on syntax issues as well as make some suggestions re: 'best practices'