如何在目录中搜索单词并将输出写入其他文件中,该文件应具有文件名,后跟该文件中的测试用例


for i in `cat /auto/qalogs/out.txt` ; do echo $i; grep -ril $i /auto/tools/; done > /auto/qalogs/out1.txt

我有一个文件(/auto/qalogs/out.txt(,每行中只有测试用例名称。我需要搜索给定目录中文件中存在的每个测试用例,输出应该是文件名和在该文件上找到的测试用例。

文件格式可以是任意的,但应该在该文件中找到文件名和测试用例。

文件名 1:在文件中找到的测试用例列表

文件名 2:在文件中找到的测试用例列表

例:

/auto/tools/file/file1.rb : tc1, tc2, tc3
/auto/tools/file/file2.rb : tc4, tc5, tc6

获取输出如下:

TC1

/auto/tools/file/file1.rb

TC3

/auto/tools/file/file2.rb

TC2

/auto/tools/file/file1.rb

如果需要任何详细信息,请告诉我

根据我对你的问题的理解,这应该让你对一种方法有一个粗略的了解——它不是世界上最有效的,但它应该相当容易理解和适应你的需求:

#!/bin/bash
# Make bash array of all filenames to look in and all test cases to look for
files=( $(find . -type f) )
cases=( $(cat testcases.txt) )
printf "################################################################################n"
printf "Looking in these files:n"
printf "%sn"  "${files[@]}"
printf "################################################################################n"
printf "n"
printf "################################################################################n"
printf "For these casesn"
printf "%sn" "${cases[@]}"
printf "################################################################################n"
printf "n"
# Look through all files in array "files"
for f in "${files[@]}" ; do
# Clear out the results for this file, so we know if we found any cases
res=""
# Check if this file contains each case
for c in "${cases[@]}" ; do
if grep -q -m1 -w "$c" "$f" ; then
# If it does, append this case to our result string "res"
res="$res $c"
fi
done
# If we found any test cases, print the filenames and the cases we found
if [ ! -z "$res" ]  ; then
echo "$f: $res"
fi
done

最新更新