将多列粘贴到同一文件中

问题描述 投票:0回答:1

我正在循环运行命令以生成具有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次并分别粘贴每个文件会很麻烦。

bash paste
1个回答
0
投票

尝试

paste -d' ' tmp2.txt tmp3.txt > tmp4.txt  &&  mv tmp4.txt tmp3.txt

您绝对不应在同一文件中进行读写。

© www.soinside.com 2019 - 2024. All rights reserved.