过滤然后合并行级别上 2 个命令的输出

问题描述 投票:0回答:1

前提:我正在使用 run-ksql-test 实用程序,它具有 commnds 输出部分中描述的输出 我有带有以下输出的测试文件 我只想用文件名映射测试状态,并且由于一些 DevOps 限制,我必须仅使用 shell 或 bash

设置:

测试文件格式:

--@test: testName1
<multiple sql statements>
--@test: testName2
<multiple sql statements>
--@test: testName3
<multiple sql statements>

命令的输出:

N number of lines with no perticular format, but wihtout ">>>"
[<timestamp>]: >>> Test Passed
N number of lines with no perticular format, but wihtout ">>>"
[<timestamp>]: >>> Test Passed
N number of lines with no perticular format, but wihtout ">>>"
[<timestamp>]: >>> Test Failed
N number of lines with no perticular format, but wihtout ">>>"

我想要的结果:


--@test: testName1    [<timestamp>]: >>> Test Passed
--@test: testName2    [<timestamp>]: >>> Test Passed
--@test: testName3    [<timestamp>]: >>> Test Failed

限制: 只有外壳代码 如果可能的话,使用 awk、grep、tr、cut、cat、sed 或 linux / unix 中默认情况下通常可用的任何其他实用程序,如果你必须的话,那很好,我可以在我这边测试

iv'e 阅读了许多类似的问题,包括 question 但我不明白 awk 能够修改它以供我使用,因为没有可以匹配两个不同行的键

这是我正在使用的代码的一小段


do
    echo "---------------- TESTING $test_file ----------------"
    run-ksql-test --test-directory $test_file --temp-folder $TEMP_FOLDER  > $LOG_DIR/$(basename $test_file)
    
    #take Names Of the tests from assembled query file
    test_names=($(cat $test_file | grep -Eo  '\-\-\@.*'))  #also tried similar variations but outupt is either one chunk or broken into words 


    #take results from the logs for that file
    test_results=($(cat $LOG_DIR/$(basename $test_file) | grep -Eoi '>>>+(Passed|Failed)'))

    for ((i=0; i<${#test_names[@]}; i++)); do
        echo "${test_names[i]} ${test_results[i]}"
    done
    sleep 3

done

这会破坏单词级别的输出并尝试回显它们,这不是所需的格式

bash shell grep ksqldb
1个回答
0
投票

除了不必要地使用 cat 之外,循环写成更易读:

for i in "${test_names[@]}"
你的算法是合理的。问题在于构建数组

test_names

grep
 的结果类似于

@test foo @test bar ...

填充你的数组

( @test foo @test bar .... )
当你只需要

( foo bar ... )
您没有告诉我们您的测试名称的语法,但如果您可以保证名称本身没有嵌入空格,您可以这样做

test_names=( $(grep -Eo '@test +[^ ]+' $test_file | tr -s ' ' | cut -f 2 -d ' ' )

tr

 将每行空格替换为单个空格,这是 
cut
 查找第二个字段所必需的。

© www.soinside.com 2019 - 2024. All rights reserved.