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 and shell scripting languages 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 03:35 PM
insert text in the middle of a file relle Shell Programming and Scripting 3 03-13-2008 03:37 PM
How to insert the 1st arg into the middle of the file boris Shell Programming and Scripting 4 04-13-2007 01:21 AM
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 05:54 AM
how to insert and delete characters in the middle of file ivancheung High Level Programming 7 10-11-2004 04:08 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-24-2006
mopimp mopimp is offline
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
  #2 (permalink)  
Old 03-26-2006
hegemaro hegemaro is offline
Registered User
  
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 134
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.
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0