我正在编写一个shell脚本来遍历字符串,并在测试用例中出现单词时回显匹配项。
我有一个看起来像
的文件/*
some build info #which are not matter of interest
*/
Test: TestCase_one
.
.
output of that testcase
#TestCase_one name will not be present in btw
.
.
TestCase_one passed
Test: TestCase_two
.
.
output of that testcase
.
.
TestCase_two passed
many testcases in this pattern
/*
some thread info
*/
在上面的文件中,我想为每个文件检查一个特定的单词 如果存在该单词,则测试用例和echo匹配,为此,我想遍历每个测试用例的文件,然后逐步移至文件末尾
有没有一种方法可以开始对在TestCase_one和TestCase_one Passed之间的单词进行grepping操作,等等?
我没有shell脚本方面的背景知识,任何建议或手册页都可以帮助我。
预期输出应为
TestCase_one
No matches
TestCase_TWo
.
.
matched lines
.
.
预先感谢
答案 0 :(得分:0)
我假设您是什么意思
在'Test:Testcase'和'TestCase _。* passed'之间搜索字符串'foo'。如果有匹配项,则打印匹配行。
您能简单地做到吗:
cat bigfile | grep -E '(foo|^Test: TestCase)'
它与您的预期输出不完全匹配(例如,不会打印“无匹配项”-不会有任何匹配项),但是它非常接近且非常简单。
如果您不希望使用所有测试用例,而只想看一个用例,那么这可能对您有用:
cat file |
perl -ne '/^Test: Testcase start/ .. /Test case end/ and /foo/ and print'
它将搜索foo
,但仅在Test: Testcase start
和Test case end
之间进行搜索。如果要在输出中包含起始行,请使用:
cat file |
perl -ne '/^Test: Testcase start/ .. /Test case end/ and /^Test: Testcase start|foo/ and print'
答案 1 :(得分:0)
前一阵子,我需要这样的东西,所以我有这个可能对您有帮助-
#!/bin/bash
checking_start=1 # flag for start of test case
processing=0 # flag for processing a test case
while IFS="" read -r p || [ -n "$p" ]; do # "p" is line in text file
if [ $checking_start -eq 1 ]; then # checking for test case start
mystartstr=`echo "$p" | grep Test: `
if [ -z "$mystartstr" ]; then # if "mystartstr" is empty, continue
echo "Non-related"
else
echo "Started processing.." # else set processing flag
processing=1 # processing now true
checking_start=0 # checking start of test case flag- false
fi
elif [ $processing -eq 1 ]; then # if processing come here
myendstr=`echo "$p" | grep TestCase_` # checking end of test case string
if [ -z "$myendstr" ]; then # if end test case string empty, continue processing
echo "Processing.." # This is where you will put your grep
else
echo "Ended processing.." # Else end processing
processing=0 # processing flag set as false
checking_start=1 # start checking for new test case start
fi
else
echo "No where.. Should never come here"
fi
done < sample.txt # Piping your text file here
我使用的sample.txt
是-
/*
some build info #which are not matter of interest
*/
Test: TestCase_one
.
.
output of that testcase
.
.
.
TestCase_one passed
Test: TestCase_two
.
.
output of that testcase
.
.
TestCase_two passed
many testcases in this pattern
/*
some thread info
*/
答案 2 :(得分:0)
awk版本:
awk <test.log -v k='MATCHWORD' -e '
$1=="Test:" { print n=t=$2; next }
t && $0~k { n=0; print }
$1==t && n { t=0; print "No matches" }
'
k
:包含要匹配的单词的变量t
:包含测试用例名称的变量n
:表示在此测试用例中是否尚未找到单词的变量$1=="Test:"
:测试用例的开始?t && $0~k
:在测试用例和行中包含单词吗?$1==t && n
:测试用例的结尾和未找到单词?