SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed?

From the command line use
(Note that these must be completed on separate lines.)
This will insert on the first line of the file.

sed '1i\
your text goes here' file_name > new_filename

This will append on the last line of the file.
sed '$a\
your text goes here' file_name > new_filename

Note also that with sed you have to direct the output and rename the file back. So 'mv new_filename file_name' when you are done with these commands.

Cool this seems to work. The problem I have now is that the string I am trying to insert contains the ' character which conflicts with the sed '1i\ line. Ordaniril they get ignored.

If I try to escape this string:

sed '1i\
EXEC PR_DbConfigStart \'test.txt\', \'10\', \'DESCRIPTION\' \
GO ' < $FILENAME > $TMPFILENAME

I get this error
syntax error at line 8 : `'' unmatched

if I try to build the string in an environment variable and rely on substitution, the string does nt seem to get substituted.

DBCONFIGSTART="EXEC PR_DbConfigStart 'test.txt', '10', 'DESCRIPTION' \nGO"

sed '1i\
$DBCONFIGSTART ' <test2.txt

producing this output

$DBCONFIGSTART
rest of file text.

Does anyone have any ideas how I can resolve this

Typically in unix you will need to interchange double quotes and single quotes - when one or the other are used within..like this.

So with the sed command use:

sed "1i\
your text has some's apostrophes's in here" file_name > new_filename

Ouch,

I get the error:
sed: command garbled: 1iEXEC PR_DbConfigStart 'test.txt', '10', 'DESCRIPTION'

when I try this.

Here is the script im using

#!/usr/bin/ksh
FILENAME=test2.txt
TMP=.$TMP
TMPFILENAME=$FILENAME$TMP
sed -e "1i\
EXEC PR_DbConfigStart 'some text', '10', 'DESCRIPTION' " < $FILENAME >$TMPFILENAME

You need a second backslash:
sed -e "1i\\

What about for the statement that appends data to the end of the file?

sed "$a\\
PR_DbConfigEnd $FILENAME " <$FILENAME >$TMPFILENAME

I get the error message:

sed: command garbled: \

If I remove the second backslash:

sed "$a\
PR_DbConfigEnd $FILENAME " <$FILENAME >$TMPFILENAME

I get this error message:

sed: command garbled: PR_DbConfigEnd test2.txt

I had a play with this and it's all about the quotes/double quotes to make the variable expand correctly. This works in bash...

FILENAME=input_file
NEWFILENAME=output_file
sed '$a\
PR_DbConfigEnd '"${FILENAME}"' ' $FILENAME > $NEWFILENAME

Notice to make the variable expand I used '"${FILENAME}"'

That's single, double ${FILENAME} double, single.

Try that.

Another solution is to escape the dollar sign:
sed "\$a\\

Hos does this work..the double backslash. I've tried numerous combinations in bash, ksh and sh. But still the only way was as i suggested above. i couldn't get it working with the \\.

Can you post the rest...i.e so I can see how the quotes etc should work around the variable in the sed text.

i.e.

FILENAME=input_file
NEWFILENAME=output_file
sed '$a\
PR_DbConfigEnd '"${FILENAME}"' ' $FILENAME > $NEWFILENAME

The trouble with posting a complete solution is that I don't exactly understand the problem completely. But here goes:

#! /usr/bin/ksh
FILENAME=input_file
NEWFILENAME=output_file
sed -e "\$a\\
PR_DbConfigEnd ${FILENAME}" <  $FILENAME > $NEWFILENAME
exit 0

I can only hope that there is some good reason for the filename to be appear inside the quotes. This will cause it to be part of the data inserted into the file. That really looks like a bug to me. If so, just delete that part. But for better or worse, I am copying from the other examples in this thread.

As for how it works, we need sed to see a line like:
$a\
but when we use double quotes, ksh processes $a as if it were a variable. But inside double quotes, a backslash removes any special power the next character has, so the \$a becomes just $a and ksh moves on. To actually get a backslash, we need \\. The first backslash is eaten by ksh as it processes the literal and this makes it leave the next backslash alone.

And bash works the same way.

Got that now..I wasn't getting the backslash before the $a.

So it is working. The only reason for putting the filename in the quotes as I did was that it was the only way I could get the end result to work. So Both actually acheive teh same result now..although I coulnn't explain why mine does! your on the other hand I do understand.

Well then, I'll take a crack at explaining your solution to you.

Inside single quotes, what you see is what you get. No character has any special meaning inside single quotes. So variables won't expand. A string like:
'$a'
will just be $a. The shell will not try to substitute the value of a variable called a. So in the line:
sed '$a\
we get exactly what we ask for. Inside single quotes, even the backslash is just another character. (And, btw, this means that there is no way to get a single quote inside single quotes...so 'you can't do that' won't work and there is no way to fix it with backslashes.)

When you want to use a variable inside single quotes, it won't work:
'something $var something'
isn't going to do it. So we must break that single quoted string into two single quoted string and put the variable between them:
'something '$var' something'
is actually good enough, but most folks will add {} whenever a variable touches non-blanks, not just other letters:
'something '${var}' something'

And finally, if the value of var has a string of blanks, we would need to protect them, so we would need single quotes around the variable:
'something '"${var}"' something'
And this get us to the syntax that you used. Your first single quoted string started on one line and finished on the next but otherwise it's pretty much what we have here.