Could you show us the first few lines of the file, and the data you wish replaced? If the data is always the same length and always in the same place, you can use dd to write it in...
---------- Post updated at 11:42 AM ---------- Previous update was at 11:37 AM ----------
An example:
$ cat textdata
This is line 1
This is line 2
This is the data I want replaced >>11111111<<
This is another line
etc etc until end of file.
$ printf "%s" 22222222 | dd conv=notrunc of=textdata seek=65 bs=1
$ cat textdata
This is line 1
This is line 2
This is the data I want replaced >>22222222<<
This is another line
etc etc until end of file.
The 'bs=1' tells it to work on a sector size of 1 byte, which lets us seek seek exactly 65 characters into the file with seek=65. The conv=notrunc is important, it tells dd not to replace the file but to just overwrite data that's already there.
---------- Post updated at 12:06 PM ---------- Previous update was at 11:42 AM ----------
Another method needing BASH 3.0 or newer:
#!/bin/bash
exec 5<hugedata
exec 6<>hugedata
# Read lines one at a time from both file descriptors.
# When we find the line we want in FD 5, FD 6 will still be at the
# previous line, allowing us to overwrite the line with it.
while read -u 5 LINE
do
# Match strings like >>12345678<< anywhere in the line
# save it in BASH_REMATCH in three segments: ...>>, 11111111, <<...
if [[ $LINE =~ ^(.*\>\>)([0-9]+)(\<\<.*)$ ]]
then
NEWLINE="${BASH_REMATCH[1]}22222222${BASH_REMATCH[3]}"
if [ "${#NEWLINE}" -ne "${#LINE}" ]
then
echo "Error, new line would be different length"
exit 1
fi
# Overwrite the line with a line of same length
echo "${NEWLINE}" >&6
exec 6>&-
exec 5>&-
echo "Found and replaced ${BASH_REMATCH[2]} with 22222222" >&2
exit 0
else
read -u 6 LINE # Keep FD 5 and FD 6 in sync
fi
done <&5
echo "Warning, didn't find any data to replace" >&2
exit 1
$ cat hugedata
This is line 1
This is line 2
This is the data I want replaced >>11111111<<
This is another line
etc etc until end of file.
$ ./datarep2.sh
$ cat hugedata
This is line 1
This is line 2
This is the data I want replaced >>22222222<<
This is another line
etc etc until end of file.
$
Both methods are able to edit early lines in the file as long as their length doesn't change, without having to read or write data afterwards at all.
The DD version would be more reliable and portable if you always know where the data to replace is.
---------- Post updated at 12:27 PM ---------- Previous update was at 12:06 PM ----------
Another thing you could do is just keep the header always separate from the huge file. When you need to feed it into something, use sed or awk or whatever to get the modified header, and cat out the rest of the file. (one of the rare useful uses of cat.)
( sed 's/orig/replacement/' < header ; cat restoffile ) | programusinghugefile