我想要这个,sed解决方案看起来不错,但是当我尝试它时似乎创建了一个巨大的临时文件。
unix command to prepend text to a file
我有大的sql文件,就像28演出,我在文件系统上没有太多空间,只想在文件的前面添加一行。如何在不占用更多文件空间的情况下执行此操作?
答案 0 :(得分:3)
不幸的是,在我见过的每个操作系统和文件系统中,通常不能像添加一样进行预先添加。有人可能会争辩说,如果数据量是底层文件系统块大小的某个倍数,文件系统可以有效地执行它,但由于通常不是这种情况,我不知道有任何实际实现了这样的功能。因此,可能,唯一的方法是通过临时文件或副本。但是,您可以使用压缩来稍微减轻空间紧张,但这需要一些预备工作,并且最终可能不适合。这些方面的东西:
1) gzip original_file.sql # or bzip2 or whatever
2) create new_file.sql with data to be prepended
3) (cat new_file.sql; zcat original_file.sql.gz) | gzip > updated_file.sql.gz
4) zcat updated_file.sql.gz | less # inspect the top of the file to make sure it looks right
5) rm original_file.sql.gz new_file.sql
6) gunzip updated_file.sql.gz # if necessary to have uncompressed text available - if not, just leave it compressed
答案 1 :(得分:0)
使用gcc编译:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
// Prepend size must be less than this value
#define bufSize 1024000
int main( int argc, char **argv )
{
FILE *fil;
unsigned char *smallBuf, *mainBuf;
size_t sReadSize, mReadSize;
long readPos = 0, writePos = 0;
int appendSize;
if( argc != 3 )
{
printf( "Usage: %s, <prepend_line> <file>\n", argv[0] );
return 1;
}
sReadSize = appendSize = strlen( argv[1] ) + 1;
smallBuf = (unsigned char *) malloc( appendSize );
mainBuf = (unsigned char *) malloc( bufSize );
if( !smallBuf || !mainBuf )
{
printf( "No memory\n" );
return 1;
}
memcpy( smallBuf, argv[1], appendSize );
smallBuf[ appendSize - 1 ] = '\n';
fil = fopen( argv[2], "rb+" );
if( !fil )
{
printf( "Cannot open file\n" );
return 1;
}
while( 1 )
{
fseek( fil, readPos, 0 );
readPos += mReadSize = fread( mainBuf, 1, bufSize, fil );
fseek( fil, writePos, 0 );
writePos += fwrite( smallBuf, 1, sReadSize, fil );
if( mReadSize < bufSize )
{
if( mReadSize > 0 )
fwrite( mainBuf, 1, mReadSize, fil );
break;
}
fseek( fil, readPos, 0 );
readPos += sReadSize = fread( smallBuf, 1, appendSize, fil );
fseek( fil, writePos, 0 );
writePos += fwrite( mainBuf, 1, mReadSize, fil );
if( sReadSize < appendSize )
{
if( sReadSize > 0 )
fwrite( smallBuf, 1, sReadSize, fil );
break;
}
}
fclose( fil );
return 0;
}
答案 2 :(得分:0)
您可以使用perl:
perl -i -n -e 'print "xxx\n$_" if $.==1;print if $.!=1' your_file