Chinaunix

标题: 如何在一个大文件的前面加一行 [打印本页]

作者: wy94    时间: 2013-06-11 17:00
标题: 如何在一个大文件的前面加一行
本帖最后由 wy94 于 2013-06-11 17:01 编辑

我有一个几百万行的文件A.txt ;和一个描述A.txt文件字段的B.txt (只有1行);
现在要将B.txt文件添加到A.txt的文件第一行。 有没有快速的方法了?

cat A.txt >> B.txt 这个很慢

$ ex A.txt <<EOF  //报内存不足
> 0r B.txt
> wq
> EOF
ex: 0602-026 No more memory for the append. Too many lines in the file.


作者: dn833    时间: 2013-06-11 17:29
本帖最后由 dn833 于 2013-06-11 17:29 编辑
  1. sed -i '1 i b.txt' a.txt
复制代码
试试看吧
作者: wy94    时间: 2013-06-11 17:48
谢谢啦,这个在linux上测试了,不过还是对整个文件进行了遍历,很慢。和cat a.txt >> b.txt速度差不多。

另外,我是在IBM上用的,sed 没有-i选项。
作者: 叶叶叶Yeah    时间: 2013-06-12 23:05
从系统提供的文件操作的API来看,是没有办法直接在文件的起始或中间部位插入一些数据的,必须要对后面的数据进行更改才行,
所以不管你用什么工具,都要对文件后面的数据进行读写(移动数据),不可能只有单行的插入操作。
作者: Shell_HAT    时间: 2013-06-13 10:14
回复 3# wy94


    在你的AIX上,cat a.txt >> b.txt需要几分钟?
作者: wy94    时间: 2013-06-13 13:45
谢谢大家了,用cat需要大约40-50秒。
>ls -l a.txt b.txt
-rw-rw-r--   1 iinscu   tellin    994989614 Jun 13 08:36 a.txt
-rw-rw-r--   1 iinscu   tellin          117 Jun 13 08:36 b.txt
>wc -l a.txt b.txt
1074836 a.txt
       1 b.txt
1074837 total
>date; cat a.txt >> b.txt; date
Thu Jun 13 08:37:06 SAUST 2013
Thu Jun 13 08:37:52 SAUST 2013

其实原始的需求是用户需要导出的数据在第一行带字段描述,而informix的unload没有追加到文件的功能
找了下面的可以实现了
unload/load, don&acute;t have the posibility of append a file, but in other mail,
someone recomend you use a pipe.
If you are in a UNIX sistem, you can use this shell scrip:
# Append a file via pipe.
mknod /tmp/mypipe p
# My first unload create the asc file
nohup 'cat /tmp/mypipe > myappendfile.asc' &
echo "unload to '/tmp/mypipe' select * to mytable1" >a.sql
dbaccess mydb a.sql
# the second unload append the asc file
nohup 'cat /tmp/mypipe >> myappendfile.asc' &
echo "unload to '/tmp/mypipe' select * to mytable1" >a.sql
dbaccess mydb a.sql
# end of shell


作者: seesea2517    时间: 2013-06-13 16:34
回复 6# wy94


    确实还是直接用数据库导出来的容易和高效。
作者: Shell_HAT    时间: 2013-06-13 18:56
回复 6# wy94


    为什么最开始发帖的时候不把原始需求说出来呢?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2