Operating System - HP-UX
1832595 Members
2987 Online
110043 Solutions
New Discussion

Insertion of line at start of a large file

 
Russell Gould
Advisor

Insertion of line at start of a large file

Hello everyone, could somone perhaps help with a query I have ....

I have a large file (2Gb). It contains many records from start to finish.

I would like to know how to insert a header line at the top of the file without generating another 2Gb file as I would have to with :

echo 'header' > masterfile
cat datafile >> masterfile
(ie 2 x 2Gb files exits at this point)
rm datafile
(leaving 1 2Gb file with the header)

Any ideas - a line editor. Some syntax would be appreciated. Thanks

On a side issue - does anyone know the maximum file size of an hfs file system. I know that you can use -o largefiles on a VxFS. HFS though ?

Thanks again
It's not a problem, it's an opportunity !
8 REPLIES 8
Paula J Frazer-Campbell
Honored Contributor

Re: Insertion of line at start of a large file

Hi

Create file with header info only then cat the two files together.

cat header orig > new

then mv newfile to orig.

HTH

Paula
If you can spell SysAdmin then you is one - anon
Victor BERRIDGE
Honored Contributor

Re: Insertion of line at start of a large file

Hi,
man merge...

Just thoughts

Victor
Paula J Frazer-Campbell
Honored Contributor

Re: Insertion of line at start of a large file

Hi

Your question of HSF file sizes :-

http://docs.hp.com/hpux/onlinedocs/5971-2383/5971-2383.html


HTH

Paula
If you can spell SysAdmin then you is one - anon
Paula J Frazer-Campbell
Honored Contributor

Re: Insertion of line at start of a large file

Also here:-

http://docs.hp.com/hpux/onlinedocs/5971-2383/5971-2383.html


Paula

If you can spell SysAdmin then you is one - anon
Justo Exposito
Esteemed Contributor

Re: Insertion of line at start of a large file

Hi,

Other aproach is:

Create a file called newfile with the new record, then do
cat oldfile >> newfile

Then if you want rename the file.

Another approach is using ed editor:
$ ed oldfile
0r newfile
w
q
$

you can use this with a file redirection, see ed man page.

Best regards,

Justo.
Help is a Beatiful word
Darrell Allen
Honored Contributor

Re: Insertion of line at start of a large file

Hi Russell,

You could try this:

cat - <1i
header line
.
w
q
EOF

This was a modification to the example from ed's man page.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Darrell Allen
Honored Contributor

Re: Insertion of line at start of a large file

Hi again,

Just tested my ed suggestion with a file that was too large to copy (filesystem would fill). It failed. In this case, ed created a temp file in the same directory that the source file was in. When the filesystem filled, the temp file was left in place. It didn't matter if I cd'ed to another filesystem or not.

Further testing found that a temp file was created even for a small input file. So I'm guessing that ed makes a copy of the file regardless of size which is the thing you didn't want to do.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Rodney Hills
Honored Contributor

Re: Insertion of line at start of a large file

The standard unix commands all will create a temp file while merging data in.

It probabily would be cheaper to buy extra disk space, but if you really want to do this then it will require some programming by doing binary reads/writes. In your favorite programming language do the following-

1) Append an empty block onto your file
2) read the last block of data and write it to the new empty block
3) read the next to the last block of data and write it to the block read in previous step
4) repeat until all blocks shuffled down, leaving the 1st block available for data.
5) insert your header into the 1st block

If anything goes wrong with this process your original file will of course be hosed.

If you really wanted to do it proper, you would have to take in account the length of the header line and shift down that much data in each block shuffled.

Good Luck

-- Rod Hills
There be dragons...