![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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
|
| Forum Sponsor | ||
|
|
|
|||
|
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. |
|||
| Google UNIX.COM |