Click to See Complete Forum and Search --> : Replace 1st Line with Text from file?


pwharff
01-30-2006, 05:48 PM
I have a huge text file (about 6 megs). I want to replace the first line of text in this file with the entire contents of another text file. Basically, I thought this would be easy by using a combination of "tail" and "cat", but since the file is so huge, it's not a pratical was of accomplishing what I need. If only there was a redirection like ">>" but would only prepend instead of append, that would work also, but I don't know of anything for that either. Anyway... any help would be greatly appreciated.

pwharff
01-30-2006, 07:00 PM
I know what I need can be accomplished with a better sed argument, but as mentioned above, the only thing I have been able to come up with that works is:

#!/bin/bash

# Remove First Line of File
sed '1,1d' PARTMAST.DBF > TempText

# Reverse Text
tail -r TempText > TempText2

# Append Text to TempText2
cat Replacement-Text >> TempText2

# Reverse it back
tail -r TempText2 > PARTMAST.DBF

All this hassle to just replace the first line of a file with different text.

bwkaz
01-30-2006, 08:07 PM
There's no syscall interface to "insert" data into a file. You have to choose between overwriting the file and appending data to the end of it.

That's why there are no shell utilities for doing this. If the OS won't let you, then there's no way to make a shell utility do it.

pwharff
01-30-2006, 08:18 PM
According to the "sed" documentation I should be able to using this syntax:

[address1][,address2]c\textstring

Replace the lines in the specified address range using textstring.


But everytime I use this:

sed '1,1c\NewText' PARTMAST.DBF

I always get this error:

sed: 1: "1,1c\NewText": extra characters after \ at the end of c command

happybunny
01-30-2006, 09:07 PM
hmmm...what about tac? that lists the file backwards....


or, why not append the small file with the big one?

cat (lines 2 thru whatever) bigfile >> smallfile?

ph34r
01-31-2006, 10:55 AM
file1 is your large file where you want to replace the first line, file2 is the file you want to replace the first line with


cat file2 > newfile
lines= expr `cat file1 | wc -l` - 1
tail -$lines file1 >> newfile

pwharff
01-31-2006, 12:36 PM
Thanks both of you for the feedback, the only problem is, I need to "prepend" the data from Replacement-Text to PARTMAST.DBF, NOT "append".

bwkaz
01-31-2006, 07:32 PM
According to the "sed" documentation I should be able to using this syntax: That's because sed operates on streams, not files. It can insert data into the middle of a stream if it has to, because it's taking input from one stream and writing it (or something else) into another, separate stream.

But if you just have one file that you want to modify, you can't do it directly. (Even sed's -i option, which was introduced with GNU sed 4.0, doesn't operate directly on a file. It creates a stream from the existing file, then writes the output to a new file, then replaces the existing file with the new file.)

Anyway, the sed "c" command requires a backslash on the line before the text that you want to insert. You don't say "sed -e '1,1c\NewText' <file >file2" -- you say:

sed -e '1,1c\
NewText' <file >file2

instead. Notice the line break after the \ character. Newlines in the "NewText" also need to have a \ character inserted before them, otherwise the c command will terminate early. (This only matters if you need to insert multiple lines.)

pwharff
02-02-2006, 04:22 PM
That's because sed operates on streams, not files. It can insert data into the middle of a stream if it has to, because it's taking input from one stream and writing it (or something else) into another, separate stream.

But if you just have one file that you want to modify, you can't do it directly. (Even sed's -i option, which was introduced with GNU sed 4.0, doesn't operate directly on a file. It creates a stream from the existing file, then writes the output to a new file, then replaces the existing file with the new file.)

Anyway, the sed "c" command requires a backslash on the line before the text that you want to insert. You don't say "sed -e '1,1c\NewText' <file >file2" -- you say:

sed -e '1,1c\
NewText' <file >file2

instead. Notice the line break after the \ character. Newlines in the "NewText" also need to have a \ character inserted before them, otherwise the c command will terminate early. (This only matters if you need to insert multiple lines.)


THANK YOU SO MUCH and please excuse my lack of knowledge with command line tools. This is perfectly what I needed. :cool: