bash – 将列粘贴到循环中的现有文件

前端之家收集整理的这篇文章主要介绍了bash – 将列粘贴到循环中的现有文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在bash循环中使用paste命令将新列添加到CSV文件中.我想重用CSV文件.目前我正在使用临时文件来完成此任务:
  1. while [ $i -le $max ]
  2. do
  3. # create text from grib2
  4. wgrib2 -d 1.$(($i+1)) -no_header myGribFile.grb2 -text tmptxt.txt
  5.  
  6. #paste to temporary file
  7. paste -d,existingfile.csv tmptxt.txt > tmpcsv.csv
  8.  
  9. #overwrite old csv with new csv
  10. mv tmpcsv.csv existingfile.csv
  11.  
  12. ((i++))
  13. done

添加一些列后,副本变得越来越慢,因为文件变得越来越大(每个tmptxt.txt大约有2 MB,增加到大约100 MB).

tmptxt.txt是一个普通的txt文件,每行有一列和一个值:

  1. 1
  2. 2
  3. 3
  4. .
  5. .

那么existingfile.csv就是

  1. 1,1,x
  2. 2,2,y
  3. 3,3,z
  4. .,.,.
  5. .,.

有没有办法使用paste命令将列添加到现有文件?或者还有其他方法吗?

谢谢

将操作拆分为2是否可行?生成所有中间文件的一步;另一个用于生成所有最终输出文件.我们的想法是避免重复读取和重写最终文件.

对脚本的更改将是这样的:

  1. while [ $i -le $max ]
  2. do
  3. n=$(printf "%05d" $i) # to preserve lexical order if $max > 9
  4. # create text from grib2
  5. wgrib2 -d 1.$(($i+1)) -no_header myGribFile.grb2 -text tmptxt$n.txt
  6. ((i++))
  7. done
  8.  
  9. #make final file
  10. paste -d,existingfile.csv tmptxt[0-9]*.txt > tmpcsv.csv
  11.  
  12. #overwrite old csv with new csv
  13. mv tmpcsv.csv existingfile.csv

猜你在找的Bash相关文章