【发布时间】:2013-11-16 19:00:09
【问题描述】:
我有来自标准输出的数据列(在我的例子中是对 mysql 的调用),我想在每个循环中附加文件中的列。我该怎么办?
Standard output:
a1
a2
....
an
保存在名为 table.dat 的文件中:
table.dat:
a1
a2
....
an
然后产生另一个输出:
Further standard output:
b1
b2
....
bn
附加到 table.dat:
table.dat:
a1 b1
a2 b2
.... ....
an bn
...等等。我可以使用粘贴,但我需要三个步骤:
line producing standard output > tmpfile;
paste prevfile tmpfile > table
mv table prevfile;
有没有更快的方法,也许是使用 awk?
这个解决方案: Add a new column to the file 生成一个空表。
【问题讨论】:
-
您可以使用
paste tablefile <(program)跳过一个临时文件。如果您有sponge,则可以添加| sponge tablefile进行就地替换,否则您只需使用临时文件并重命名每次迭代即可。 -
[mysql call] | paste table - | sponge table正是我所需要的,谢谢!令人难以置信的是moreutilsdebian 包中的这个“sponge”:我从来没有听说过!