一些精度:
- 常规 awk,而不是 gawk
- AIX 6.1 和旧 shell:GNU bash,版本 2.05b.0(1)
我试图以正确的顺序显示一些内容,因此我通过 shell 的“sort”和“uniq”管道输出...但我无法按照我编写的顺序获取输出。
麻烦的代码:
egrep -i "something_FREQUENT" BIGFILE | sort | awk -F',' '
{ servs=servs $1 " " ; groupe=groupe "\n " $2 ; }
END { print "Groupes (alphabetical order):" ;
printf groupe "\n" | "grep . | sort | uniq | xargs echo ";
print ; rem="extra newline to help buffering... does NOT help.";
system(""); rem="supposed to force flush of stdout... does NOT help.";
print "Servers:"
printf "%s\n", servs;
} ' # if I add a final: "| cat" here (after "'"), does NOT help?... see last example
它输出:
Groupes (alphabetical order):
Servers:
( here a quite long list of severs... maybe until "buffer size" length? )
( etc.... but at some point :)(HERE_THE_groupe_OUTPUT)(rest of the servers here....)
??...我不知所措:似乎如果我添加:system("");在显示服务器的行之前,它也没有帮助......
同一系统上的一些相关测试:
1)类似于我的脚本:通过系统调用的“长”输出,出现乱码:
prompt$: yes "test1" | head -10 | awk '
{ all=all "\n" $0; }
END { print "first line:" ;
printf all "\n" | "sort ";
system("");
print "last line" ;
}'
first line:
last line
test1
test1
test1
test1
test1
test1
test1
test1
test1
test1
# notice: the order is mixed: the shell command output is last.
2) 相同,但这次我们添加最后一个 '| cat' :它神奇地重新排序输出......但是如果我在上面的脚本中执行相同的操作,这不会有帮助......
prompt$: yes "test2" | head -10 | awk '
{ all=all "\n" $0; }
END { print "first line:" ;
printf all "\n" | "sort ";
system("");
print "last line" ;
}' | cat # ONLY ADDITION: pipe awk through cat... this HELPS here (but NOT in my script!... ??)
first line:
test2
test2
test2
test2
test2
test2
test2
test2
test2
test2
last line
# notice: the order is OK!
答案1
您需要关闭 awk 内的管道才能将其刷新。对于第二个例子,请执行以下操作:
yes "test1" | head -10 | awk '
{ all=all "\n" $0; }
END { print "first line:" ;
printf all "\n" | "sort ";
close("sort ");
print "last line" ;
}'
为了使其在第一个示例中更加明显,请执行以下操作
cmd = "grep . | sort | uniq | xargs echo ";
printf groupe "\n" | cmd;
close(cmd);