高效的方式在一个非常大的文件的开头添加两行 [英] Efficient way to add two lines at the beginning of a very large file

查看:303
本文介绍了高效的方式在一个非常大的文件的开头添加两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组非常大的(几个GB的每个)文本文件。我需要在每个文件的开头添加两行。



我尝试使用sed使用以下命令

  sed -i'1iFirstLine'
sed -i'2iSecondLine'

sed的问题是它循环遍历整个文件,即使在开始时只添加两行,因此它需要很多时间。


$ b $

解决方案

您应该尝试下面的方法: / p>

  echo1iFirstLine> newfile.txt 
echo2iSecondLine>> newfile.txt
cat oldfile.txt>> newfile.txt
mv newfile.txt oldfile.txt


I have a group of very large (a couple of GB's each) text files. I need to add two lines at the beginning of each of these files.

I tried using sed with the following command

sed -i '1iFirstLine'
sed -i '2iSecondLine'

The problem with sed is that it loops through the entire file, even if had to add only two lines at the beginning and therefore it takes lot of time.

Is there an alternate way to do this more efficiently, without reading the entire file?

解决方案

You should try

echo "1iFirstLine" > newfile.txt
echo "2iSecondLine" >> newfile.txt
cat oldfile.txt >> newfile.txt
mv newfile.txt oldfile.txt

这篇关于高效的方式在一个非常大的文件的开头添加两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆