Home Man
Today's Posts

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

SED- Insert text at top of file

👤 Login to reply

Thread Tools Search this Thread
# 1  
Old 06-20-2002
SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed?
# 2  
Old 06-20-2002
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.
# 3  
Old 06-26-2002

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\' \

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

rest of file text.

Does anyone have any ideas how I can resolve this
# 4  
Old 06-26-2002
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
# 5  
Old 06-27-2002

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

when I try this.

Here is the script im using

sed -e "1i\
EXEC PR_DbConfigStart 'some text', '10', 'DESCRIPTION' " < $FILENAME >$TMPFILENAME
# 6  
Old 06-27-2002
You need a second backslash:
sed -e "1i\\
# 7  
Old 07-01-2002
What about for the statement that appends data to the end of the file?

sed "$a\\

I get the error message:

sed: command garbled: \

If I remove the second backslash:

sed "$a\

I get this error message:

sed: command garbled: PR_DbConfigEnd test2.txt
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert date/time header at top of file kramer65 Shell Programming and Scripting 2 03-03-2014 03:25 AM
Insert string in binary file at top param_it UNIX for Advanced & Expert Users 4 05-31-2011 06:15 AM
Using sed to insert text file at first line psve Shell Programming and Scripting 18 03-29-2011 03:44 AM
Sed insert text at first line of empty file DC Slick Shell Programming and Scripting 4 02-18-2011 02:17 AM
Insert text file only after the first match with SED PhoenixONE Shell Programming and Scripting 1 02-06-2010 09:53 AM
best way to insert a line at the top of a file? fedora Shell Programming and Scripting 7 02-09-2009 10:01 PM
Need to insert new text and change existing text in a file using SED iamgeethuj Shell Programming and Scripting 4 11-25-2008 02:59 AM
how do I insert argument into TOP of file using vi? rprules UNIX for Dummies Questions & Answers 6 08-04-2007 05:33 PM
insert text into top of file jimbob Shell Programming and Scripting 1 09-22-2006 05:46 PM
Insert Text With Sed steveramsey UNIX for Dummies Questions & Answers 5 02-09-2006 08:48 AM

All times are GMT -4. The time now is 12:08 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
Show Password