bash - bash 将多列粘贴到同一个文件中
问题描述
我在循环中运行一个命令来生成一个包含 n 列的文件。为方便起见,让我们说 10 以使其简单。例如:
# Run the command to produce the data. Pipe the output to a temp file
${command here} > tmp1.txt
# We have a bit of superfluous information, so just pipe the output we need
tail -n +7 tmp1.txt | awk '{print $7}' > tmp2.txt
上面的命令只会生成一个包含 1 列数据的文件。
问题是,我希望这一切都包含在一个循环中运行 n 次。因此,tmp3.txt(输出)应该有 n 列。当我尝试添加以下命令时:
for i in {1..10}
do
${command here} > tmp1.txt
tail -n +7 tmp1.txt | awk '{print $7}' > tmp2.txt
if [[ ! -f tmp3.txt ]]
then
cp tmp2.txt tmp3.txt
else
paste -d' ' tmp2.txt tmp3.txt >> tmp3.txt
fi
done
该文件爆炸了,甚至在 3 或 4 次迭代后我的内存都用完了。我将如何实现预期目标?写出 30-50 次并单独粘贴每个文件会很麻烦。
解决方案
尝试
paste -d' ' tmp2.txt tmp3.txt > tmp4.txt && mv tmp4.txt tmp3.txt
你永远不应该在同一个文件中读写。
推荐阅读
- sapui5 - 如何静态添加控件到项目
- odata - UI5 在 Value 属性中访问其他模型路径数据
- biml - Biml Book 第 3 章,无法通过 BimlExpress 2018 从 BIML 生成 SSIS 包
- javascript - 如果我的购物车打开 HTML 发票,我应该考虑哪些安全问题?
- c - 打印在两侧最多浮动四个位置
- shiny - plotlyOutput 显示在 Shiny Apps 中刷新之前的上一个绘图
- python-3.x - 在熊猫数据框 groupby 中使用百分位数去除异常值
- sql-server - 如果 SQL Server 2008 r2 中存在,则删除列
- spring-boot - 关闭或崩溃时如何在 PCF 中自动重启单个实例
- node.js - 使用 AJAX 请求从 Node JS 服务器获取数据