类似于以下文件:
档案A:
module modA;
include file B;
include file C;
[more stuff]
end module;
Run Code Online (Sandbox Code Playgroud)
文件B:
value x=5;
value y=7;
include file D;
[etc.]
Run Code Online (Sandbox Code Playgroud)
文件C:
value z=10;
value title="A string";
[etc.]
Run Code Online (Sandbox Code Playgroud)
文件D:
value x1=15;
[etc.]
Run Code Online (Sandbox Code Playgroud)
我希望能够用来gawk捕捉这些include file行,将它们打印出来,然后gawk通读找到的新文件,或者至少将其添加到文件列表中,就像在命令行中一样。输出看起来像这样:
A: B
A: C
A: D
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何在gawk不知道先验文件B,C和D的情况下通读文件?
Assuming every line of file is a file name, this will execute do stuff on every file whose name is contained in file.
awk 'NR==FNR{ ARGV[ARGC] = $0; ARGC++; next } { do stuff }' file
Run Code Online (Sandbox Code Playgroud)
If this isn't waht you want edit your question to describe your requirements better (and lose all the domain-specific terminology like specialty dependency generator for Motif UIL files as that just obfuscates your question, it's all just records and fields or rows and columns to the rest of us) and to provide specific sample input and expected output.
Given your updated question, see the example I give of recursive-descent parsing at http://awk.info/?tip/getline:
awk 'function read(file) {
while ( (getline < file) > 0) {
if ($1 == "include") {
read($2)
} else {
print > ARGV[2]
}
}
close(file)
}
BEGIN{
read(ARGV[1])
ARGV[1]=""
close(ARGV[2])
}1' file1 tmp
Run Code Online (Sandbox Code Playgroud)
The above not only expands all the lines that say "include subfile", but by writing the result to a tmp file, resetting ARGV[1] (the highest level input file) and not resetting ARGV[2] (the tmp file), it then lets awk do any normal record parsing on the result of the expansion since that's now stored in the tmp file. If you don't need that, just do the "print" to stdout and remove any other references to a tmp file or ARGV[2]. In this case, since it's convenient to use $1 and $2, and no other part of the program references any builtin variables, getline was used without populating an explicit variable. This method is limited in its recursion depth to the total number of open files the OS permits at one time.
Let us know if you cant figure out how to tweak that to suit your needs.