将文件名添加到粘贴文本上方的shell脚本中的附加文本文件中

时间:2022-12-11 13:53:21

Working on a script (Mac OSX Yosemite, working in Terminal), where I analyse a lot of files and where I want to add the output of the script in one text file.

使用脚本(Mac OSX Yosemite,在终端中工作),我分析了很多文件以及我想在一个文本文件中添加脚本输出的位置。

gawk -f ./user/filter.dat ./user/outputpre1.out >> ./user/outputpost.out
gawk -f ./user/filter.dat ./user/outputpre2.out >> ./user/outputpost.out

For interpretation finding the source of the output is very useful. The output I have right now is all just rows, like this:

对于解释,找到输出的来源非常有用。我现在的输出只是行,如下所示:

ROW1:OUTPUTOUTPUT
ROW2:OUTPUTOUTPUT
ROW8:OUTPUTOUTPUT
ROW10:OUTPUTOUTPUT

What I want:

我想要的是:

**Output from file outputpre1.out
ROW1:OUTPUTOUTPUT
ROW2:OUTPUTOUTPUT

**Output from file outputpre2.out
ROW8:OUTPUTOUTPUT
ROW10:OUTPUTOUTPUT

Is that possible via gawk?

这可能是通过gawk?

1 个解决方案

#1


You do not show the filter.dat script, but gawk does know the name of the current input-file, which can be incorporated into the logic of your script (see manual):

您没有显示filter.dat脚本,但gawk确实知道当前输入文件的名称,该名称可以合并到脚本的逻辑中(参见手册):

FILENAME

The name of the current input file. If no files are specified on the command line, the value of FILENAME is “-”. However, FILENAME is undefined inside the BEGIN block (unless set by getline).

当前输入文件的名称。如果在命令行中未指定文件,则FILENAME的值为“ - ”。但是,在BEGIN块内部未定义FILENAME(除非由getline设置)。

How it would be incorporated depends on how filter.dat is organized. The script would have to set a variable (which would be tested on repeated uses) to ensure that it prints the filename once only. Something like this fragment:

如何合并取决于filter.dat的组织方式。该脚本必须设置一个变量(将在重复使用时进行测试),以确保它只打印一次文件名。类似这样的片段:

BEGIN {
    first = 1
}
... (just before printing rows)
if (first == 1) {
    printf "\n**Output from file %s\n", FILENAME;
    first = 0;
}

#1


You do not show the filter.dat script, but gawk does know the name of the current input-file, which can be incorporated into the logic of your script (see manual):

您没有显示filter.dat脚本,但gawk确实知道当前输入文件的名称,该名称可以合并到脚本的逻辑中(参见手册):

FILENAME

The name of the current input file. If no files are specified on the command line, the value of FILENAME is “-”. However, FILENAME is undefined inside the BEGIN block (unless set by getline).

当前输入文件的名称。如果在命令行中未指定文件,则FILENAME的值为“ - ”。但是,在BEGIN块内部未定义FILENAME(除非由getline设置)。

How it would be incorporated depends on how filter.dat is organized. The script would have to set a variable (which would be tested on repeated uses) to ensure that it prints the filename once only. Something like this fragment:

如何合并取决于filter.dat的组织方式。该脚本必须设置一个变量(将在重复使用时进行测试),以确保它只打印一次文件名。类似这样的片段:

BEGIN {
    first = 1
}
... (just before printing rows)
if (first == 1) {
    printf "\n**Output from file %s\n", FILENAME;
    first = 0;
}