加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 01:35:27 所属栏目:安全 来源:网络整理
导读:我在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 f
我在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读