Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-20-2002
MBGPS MBGPS is offline
Registered User
 
Join Date: Jun 2002
Last Activity: 1 October 2002, 12:38 PM EDT
Location: London Uk
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed?
Sponsored Links
    #2  
Old Unix and Linux 06-20-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 06-26-2002
MBGPS MBGPS is offline
Registered User
 
Join Date: Jun 2002
Last Activity: 1 October 2002, 12:38 PM EDT
Location: London Uk
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
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 Unix and Linux 06-26-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #5  
Old Unix and Linux 06-27-2002
MBGPS MBGPS is offline
Registered User
 
Join Date: Jun 2002
Last Activity: 1 October 2002, 12:38 PM EDT
Location: London Uk
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old Unix and Linux 06-27-2002
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Advisor  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 470 Times in 271 Posts
You need a second backslash:
sed -e "1i\\
Sponsored Links
    #7  
Old Unix and Linux 07-01-2002
MBGPS MBGPS is offline
Registered User
 
Join Date: Jun 2002
Last Activity: 1 October 2002, 12:38 PM EDT
Location: London Uk
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert Text On file harshakusam Shell Programming and Scripting 6 08-20-2009 02:51 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 to insert some constant text at beginig of each line within a text file. Muhammad Afzal Shell Programming and Scripting 4 10-30-2008 11:01 PM
insert some text to a file log bucci Shell Programming and Scripting 4 05-09-2007 07:19 AM
insert text into top of file jimbob Shell Programming and Scripting 1 09-22-2006 05:46 PM



All times are GMT -4. The time now is 01:07 PM.