The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to insert text in the middle of a file kartikkumar84@g UNIX for Dummies Questions & Answers 6 05-10-2008 11:35 AM
insert text in the middle of a file relle Shell Programming and Scripting 3 03-13-2008 11:37 AM
How to insert the 1st arg into the middle of the file boris Shell Programming and Scripting 4 04-12-2007 09:21 PM
How to insert text into first line of the file and middle of the file? ali hussain Shell Programming and Scripting 3 03-05-2007 01:54 AM
how to insert and delete characters in the middle of file ivancheung High Level Programming 7 10-11-2004 12:08 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-24-2006
Registered User
 

Join Date: Mar 2006
Posts: 14
insert text into the middle of a original file

how do u insert text into a specific place in a file, say the middle for example, without changing the name for that file
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-26-2006
Registered User
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 130
Inserting text into a file either through standard UNIX commands such as sed(1) or awk(1) or through use of a customized program will require, at least, a temporary file. An example of the former would be:

FILE=filename
sed -e "s/this text/for that/" 1>/tmp/$FILE.tmp && cat /tmp/$FILE.tmp > $FILE && rm /tmp/$FILE.tmp

A C example is a bit more complex (and will not be attempted without additional specifications) but would roughly work as:

1 ) Open current file
2 ) Open temporary file from tmpnam(3C)
3 ) Read current file making changes as necessary and write to temporary
4 ) Rewind both files
5 ) Read temporary file writing to current file
6 ) Resize/truncate current file (Necessary if temporary file is smaller than original, not required otherwise; good practice to always do anyway)
7 ) Close both files

I hope this is helpful.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0