我有很多文件,每个文件都在一个目录中。我的脚本应该:
在文件中查找字符串。假设文件名为“results”,字符串为“average”。
然后将字符串行上的所有其他内容附加到另一个名为“allResults”的文件中。运行脚本后,文件“allResults”应包含与“结果”文件一样多的行,例如
allResults.txt(我想要的):
Everything on the same line as the string, "average" in directory1/results
Everything on the same line as the string, "average" in directory2/results
Everything on the same line as the string, "average" in directory3/results
...
Everything on the same line as the string, "average" in directory-i/results
我的脚本可以找到我需要的东西。当脚本正在运行时,我通过在“allResults.txt”上执行“cat”并在“allResults.txt”的父目录上执行“ls -l”来检查。即,我可以在屏幕上看到“查找”的输出,并且“allResults.txt”的大小会短暂增加,然后回到0。问题是脚本完成后“allResults.txt”为空。因此“查找”的结果不会被附加/添加到“allResults.txt”中。它们正在被覆盖。这是我的脚本(我使用“gsed”,GNU sed,因为我是 Mac OSX Sierra 用户):
#!/bin/bash
# Loop over all directories, find.
let allsteps=100000
for ((step=0; step <= allsteps; step++)); do
i=$((step));
findme="average"
find ${i}/experiment-1/results.dat -type f -exec gsed -n -i "s/${findme}//p" {} \; >> allResults.txt
done
请注意,我在这里的示例中使用了“>>”,因为我读到它附加了(这是我想要的 - 与所有文件中的“查找”匹配的所有行的列表),而“>”会覆盖。但是,在这两种情况下(当我使用“>”或“>>”时),我最终都会得到一个空allResults.txt文件。