![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Insert Text at begining of file | netwolf | High Level Programming | 3 | 05-27-2008 12:20 PM |
| How to insert text in the middle of a file | kartikkumar84@g | UNIX for Dummies Questions & Answers | 6 | 05-10-2008 02:35 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 |
| insert text into a dinamic file | sunbird | UNIX for Dummies Questions & Answers | 2 | 11-05-2001 10:58 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|