Bash 迭代文件,通过标准输出和管道将文件的最后一行和文件名保存到文本文件中

Bash 迭代文件,通过标准输出和管道将文件的最后一行和文件名保存到文本文件中,bash,shell,awk,Bash,Shell,Awk,昨天我问了一个问题:如何将控制台输出保存在文件中(请参阅)。现在我迭代文件,编译它们。这是我的命令: for REPORT in Test_Basic_*.scala; do scalac -Xplugin:divbyzero.jar $REPORT | awk 'END{print $REPORT} END{print}' >> output.txt done 我想保存编译的最后输出和文件名。在上面的示例中,只保存$REPORT,但我想引用迭代变量的名称 例如,我有文件T

昨天我问了一个问题:如何将控制台输出保存在文件中(请参阅)。现在我迭代文件,编译它们。这是我的命令:

for REPORT in Test_Basic_*.scala; do
    scalac -Xplugin:divbyzero.jar $REPORT | awk 'END{print $REPORT} END{print}' >> output.txt
done
我想保存编译的最后输出和文件名。在上面的示例中,只保存$REPORT,但我想引用迭代变量的名称

例如,我有文件Test_Condition.scala并运行上面的命令:

for REPORT in Test_Basic_*.scala; do
    scalac -Xplugin:divbyzero.jar $REPORT | awk 'END{print $REPORT} END{print}' >> output.txt;
done
然后
scalac-Xplugin:divbyzero.jar$REPORT
生成以下输出:

You have overwritten the standard meaning
Literal:()
rhs type: Int(1)
Constant Type: Constant(1)
We have a literal constant
List(localhost.Low)
Constant Type: Constant(1)
Literal:1
rhs type: Int(2)
Constant Type: Constant(2)
We have a literal constant
List(localhost.High)
Constant Type: Constant(2)
Literal:2
rhs type: Boolean(true)
Constant Type: Constant(true)
We have a literal constant
List(localhost.High)
Constant Type: Constant(true)
Literal:true
LEVEL: H
LEVEL: H
okay
LEVEL: H
okay
false
symboltable: Map(a -> 219 | Int | object TestIfConditionWithElseAccept2 | normalTermination | L, c -> 221 | Boolean | object TestIfConditionWithElseAccept2 | normalTermination | H, b -> 220 | Int | object TestIfConditionWithElseAccept2 | normalTermination | H)
pc: Set(L, H)
现在我想保存在output.txt
Test\u Condition.scala
(文件名)和
pc:Set(L,H)
(编译输出的最后一行)。使用上面的命令,我只保存
$REPORT pc:Set(L,H)

如果这个解释很复杂,就告诉我。谢谢你的大力支持


Matthias

如果我正确解释了您的问题,您只需要在一堆文件上执行命令,然后存储文件名和命令输出的最后一行。这应该起作用:

: > output # clear out the output file for file in Test_Basic_*.scala; do printf "$file: " >> output scalac ... | awk 'END { print }' >> output done :>输出#清除输出文件 用于Test_Basic.*.scala中的文件;做 printf“$file:”>>输出 斯卡拉克…|awk'END{print}'>>输出 完成 您只能获得文本“$REPORT”,因为您的awk脚本包含在单引号中,这样可以防止shell替换
REPORT
变量

试试这个:

scalac ... "$REPORT" | awk -v filename="$REPORT" 'END {print filename; print}' >> output
-v
选项设置名为
filename
的awk变量,以保存shell的
报告
变量的值


另外,总是引用shell变量也是一个很好的经验法则(除非你特别想要忽略它们的副作用)。

正是我想要的,谢谢你救了我一天,我学会了“printf”命令。你可以将重定向移到最后,你不必先清除文件:
for。。。完成>输出
另一个选项是使用
ENVIRON
awk变量<代码>…|awk'END{print ENVIRON[“SHELL”];print}'@jamessan,是的,但必须先导出变量。