SED- Insert text at top of file

Tags
shell scripts

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
Network

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
# 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
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
# 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\\
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
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using sed to insert text between lines tsu3000 Shell Programming and Scripting 3 06-29-2016 06:44 AM
Insert date/time header at top of file kramer65 Shell Programming and Scripting 2 03-03-2014 04:25 AM
How do I insert text with sed ? Paul Walker Shell Programming and Scripting 9 03-11-2013 01:41 PM
Insert text using sed lawsongeek Shell Programming and Scripting 7 03-16-2012 12:07 PM
Insert string in binary file at top param_it UNIX for Advanced & Expert Users 4 05-31-2011 07:15 AM
Using sed to insert text file at first line psve Shell Programming and Scripting 18 03-29-2011 04:44 AM
Sed insert text at first line of empty file DC Slick Shell Programming and Scripting 4 02-18-2011 03:17 AM
Insert text with Sed (in various positions) </kida> Shell Programming and Scripting 4 01-12-2011 08:36 PM
Insert text file only after the first match with SED PhoenixONE Shell Programming and Scripting 1 02-06-2010 10:53 AM
sed insert text at particular line action_owl Shell Programming and Scripting 2 08-24-2009 03:17 AM
best way to insert a line at the top of a file? fedora Shell Programming and Scripting 7 02-09-2009 11: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 03:59 AM
how do I insert argument into TOP of file using vi? rprules UNIX for Dummies Questions & Answers 6 08-04-2007 06:33 PM
insert text into top of file jimbob Shell Programming and Scripting 1 09-22-2006 06:46 PM
Insert Text With Sed steveramsey UNIX for Dummies Questions & Answers 5 02-09-2006 09:48 AM
All times are GMT -4. The time now is 08:04 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?