我很好奇,在以下情况下更快。 我有大约2MB的输出文件和数千行(在15k – 50k之间的任何地方)。
我正在寻找一个string在文件的末尾(最后10行)左右。 我多次执行此操作,有时使用相同的最后10行文件,以及多个文件。
我很好奇以下哪个是最快最有效的:
tail
最后10行,保存为一个variables。 当我需要grep
或检查一个string时,在输出中echo
该variables和grep
grep
东西,首先tail
输出文件,然后pipe
和grep
的输出 grep
整个文件。 选项1)
if [ -f "$jobFile".out ]; then { output=$(tail -n 10 "$jobFile".out) !((echo "$output" | grep -q "Command exited with non-zero status" ) || (echo "$output" | grep -q "Error termination via Lnk1e")) && continue { output "$(grep $jobID $curJobsFile)" sed -i "/$jobID/d" "$jobIDsWithServer" } fi
选项2)
if [ -f "$jobFile".out ]; then { !((tail -n 10 "$jobFile".out | grep -q "Command exited with non-zero status" ) || (tail -n 10 "$jobFile".out | grep -q "Error termination via Lnk1e")) && continue { output "$(grep $jobID $curJobsFile)" sed -i "/$jobID/d" "$jobIDsWithServer" } fi
选项3)
if [ -f "$jobFile".out ]; then { !((grep -q "Command exited with non-zero status" "$jobFile".out) || (grep -q "Error termination via Lnk1e" "$jobFile".out)) && continue { output "$(grep $jobID $curJobsFile)" sed -i "/$jobID/d" "$jobIDsWithServer" } fi
选项2使用尾巴两次,所以可能会比1稍慢。两者比选项3要快得多。
你可以做的另一件事是:
if [ -f "$jobFile".out ]; then { !(tac "$jobFile".out | grep -E -m1 -q "(Command exited with non-zero status|Error termination via Lnk1e)") && continue { output "$(grep $jobID $curJobsFile)" sed -i "/$jobID/d" "$jobIDsWithserver" } fi
这将以相反的顺序输出文件,grep将在第一次匹配后停止。 此外,它将同时搜索两个搜索条件,如果与第一项不匹配,则不必再次grep两次。
为什么不是这样的:
if tail -f "$jobfile.out" \ | grep -F -e "Command exited with non-zero status" -e "Error termination via Lnk1e" then output "$(grep $jobID $curJobsFile)" sed -i "/$jobID/d" "$jobIDsWithserver" fi
这样,您就可以实时查看尾部的输出,直到找到所需的内容。
在不使用正则表达式的情况下,在grep中使用-F
标志会使速度更快。