我在bash循环中使用paste命令将新列添加到CSV文件中.我想重用CSV文件.目前我正在使用临时文件来完成此任务:
- while [ $i -le $max ]
- do
- # create text from grib2
- wgrib2 -d 1.$(($i+1)) -no_header myGribFile.grb2 -text tmptxt.txt
- #paste to temporary file
- paste -d,existingfile.csv tmptxt.txt > tmpcsv.csv
- #overwrite old csv with new csv
- mv tmpcsv.csv existingfile.csv
- ((i++))
- done
添加一些列后,副本变得越来越慢,因为文件变得越来越大(每个tmptxt.txt大约有2 MB,增加到大约100 MB).
tmptxt.txt是一个普通的txt文件,每行有一列和一个值:
- 1
- 2
- 3
- .
- .
那么existingfile.csv就是
- 1,1,x
- 2,2,y
- 3,3,z
- .,.,.
- .,.
有没有办法使用paste命令将列添加到现有文件?或者还有其他方法吗?
谢谢
将操作拆分为2是否可行?生成所有中间文件的一步;另一个用于生成所有最终输出文件.我们的想法是避免重复读取和重写最终文件.
对脚本的更改将是这样的:
- while [ $i -le $max ]
- do
- n=$(printf "%05d" $i) # to preserve lexical order if $max > 9
- # create text from grib2
- wgrib2 -d 1.$(($i+1)) -no_header myGribFile.grb2 -text tmptxt$n.txt
- ((i++))
- done
- #make final file
- paste -d,existingfile.csv tmptxt[0-9]*.txt > tmpcsv.csv
- #overwrite old csv with new csv
- mv tmpcsv.csv existingfile.csv