从空间标记处输出阵列时,bash弦被切断

发布于 2025-01-20 14:46:13 字数 1020 浏览 2 评论 0原文

因此,我正在浏览日志文件,并从上述日志文件中提取信息,例如设备名称,位置和日志时间。它看起来像以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2025-01-27 14:46:13

设置:

$ cat hacker.txt
edge2.mco1-9 Jun 11:32:24 GMT %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success
$ y=edge2
$ z=mco1

GREP命令:

$ grep "$y\.$z\-.*\%" hacker.txt | cut -f2 -d "-" | cut -f1 -d% | head -n 1                                                                    
9 Jun 11:32:24 GMT

将其加载到testing []数组中:

$ testing=( $(grep "$y\.$z\-.*\%" hacker.txt | cut -f2 -d "-" | cut -f1 -d% | head -n 1) )
$ typeset -p testing
declare -a testing=([0]="9" [1]="Jun" [2]="11:32:24" [3]="GMT")

运行测试:

$ counter=1
$ [[ ${testing[$counter]} =~ [^0-9] ]] && echo "success"
success

# turn on debugging to see what we're testing:
$ set -xv                                                  # turn on debugging

$ [[ ${testing[$counter]} =~ [^0-9] ]] && echo "success"
[[ ${testing[$counter]} =~ [^0-9] ]] && echo "success"
+ [[ Jun =~ [^0-9] ]]                                      # contents of testing[1] == 'Jun'
+ echo success
success

$ set +xv                                                  # turn off debugging

到目前为止还不错。

OP表示所需的输出应该看起来像:

9 Jun 11:32:24 GMT

但是当前echo命令有一个问题:

echo $testing                                            

$ testing是一个数组;没有索引bash处理$ testing$ {testing [0]}相同,在这种情况下,输出9(请参阅类型-P Testing的输出 - 上面);要输出数组的整个内容,op可以使用:

$ echo "${testing[@]}"
9 Jun 11:32:24 GMT

net结果:op应该能够将echo $ testing更改为echo“ $ {testing [@]}” to获取所需的输出。


OP提到了代码复杂性(加载testing []数组,比较测试[1] ???)仅是由于grep 找到比赛。

我们可以通过以下方式替换循环的的胆量来简化代码:

    # no need for array, just capture datetime stamp:

    dtstamp=$(grep "$y\.$z\-.*\%" hacker.txt | cut -f2 -d "-" | cut -f1 -d% | head -n 1)

    # is length of 'dtstamp' non-zero?

    if [[ -n "${dtstamp}" ]]
    then
        echo "${z}"
        echo "${dtstamp}"
    fi

如果目的是仅打印$ {z}grep 找到匹配项(即,OP不需要打印$ {dtStamp}),我们可以将代码简化为z中唯一的命令>循环是:

grep "$y\.$z\-.*\%" hacker.txt >/dev/null && echo "${z}"

演示:

$ z=mco1
$ grep "$y\.$z\-.*\%" hacker.txt >/dev/null && echo "${z}"
mco1

$ z=xxxx
$ grep "$y\.$z\-.*\%" hacker.txt >/dev/null && echo "${z}"
              <<<=== no output

注释:

  • 还可以从当前代码
  • OP中删除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:

$ cat hacker.txt
edge2.mco1-9 Jun 11:32:24 GMT %SEC_LOGIN-5-LOGIN_SUCCESS: Login Success
$ y=edge2
$ z=mco1

The grep command:

$ grep "$y\.$z\-.*\%" hacker.txt | cut -f2 -d "-" | cut -f1 -d% | head -n 1                                                                    
9 Jun 11:32:24 GMT

Loading this into the testing[] array:

$ testing=( $(grep "$y\.$z\-.*\%" hacker.txt | cut -f2 -d "-" | cut -f1 -d% | head -n 1) )
$ typeset -p testing
declare -a testing=([0]="9" [1]="Jun" [2]="11:32:24" [3]="GMT")

Running the test:

$ counter=1
$ [[ ${testing[$counter]} =~ [^0-9] ]] && echo "success"
success

# turn on debugging to see what we're testing:
$ set -xv                                                  # turn on debugging

$ [[ ${testing[$counter]} =~ [^0-9] ]] && echo "success"
[[ ${testing[$counter]} =~ [^0-9] ]] && echo "success"
+ [[ Jun =~ [^0-9] ]]                                      # contents of testing[1] == 'Jun'
+ echo success
success

$ set +xv                                                  # turn off debugging

So far so good.

OP has stated the desired output should look like:

9 Jun 11:32:24 GMT

But the current echo command has an issue:

echo $testing                                            

$testing is an array; without an index bash treats $testing the same as ${testing[0]} which in this case outputs 9 (see the output from typeset -p testing - above); to output the entire contents of the array OP can use:

$ echo "${testing[@]}"
9 Jun 11:32:24 GMT

Net result: OP should be able to change echo $testing to echo "${testing[@]}" to obtain the desired output.


OP has mentioned the code complication (loading testing[] array, comparing testing[1] ???) is due to only printing output if grep finds a match.

We can streamline the code a bit by replacing the guts of the for z loop with:

    # no need for array, just capture datetime stamp:

    dtstamp=$(grep "$y\.$z\-.*\%" hacker.txt | cut -f2 -d "-" | cut -f1 -d% | head -n 1)

    # is length of 'dtstamp' non-zero?

    if [[ -n "${dtstamp}" ]]
    then
        echo "${z}"
        echo "${dtstamp}"
    fi

If the objective is to only print ${z} when grep 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 the for z loop is:

grep "$y\.$z\-.*\%" hacker.txt >/dev/null && echo "${z}"

Demo:

$ z=mco1
$ grep "$y\.$z\-.*\%" hacker.txt >/dev/null && echo "${z}"
mco1

$ z=xxxx
$ grep "$y\.$z\-.*\%" hacker.txt >/dev/null && echo "${z}"
              <<<=== no output

NOTES:

  • can also remove the counter=1 from the current code
  • OP should consider cutting-n-pasting their code (along with the appropriate shebang, eg, #!/bin/bash) into shellcheck.net; this will report on syntax issues as well as make some suggestions re: 'best practices'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文