Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to insert text in the middle of a file Post 302193582 by kartikkumar84@g on Friday 9th of May 2008 08:07:06 PM
Old 05-09-2008
How to insert text in the middle of a file w/o SED or AWK!!

tht dint wrk as i am trying to do this using a script..im pasting the script

#!bin/bash

echo "Enter a line of text";
read text;

echo "Enter the file path to append to";
read path;

nfline=$(wc -l $path);
middle=$($nfline/2);


head -$middle $path > tmpfile;
$text >> tmpfile;
tail -$middle $path >> tmpfile;

mv tmpfile $path;

more $path;

Last edited by kartikkumar84@g; 05-09-2008 at 10:06 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

how to insert and delete characters in the middle of file

I have a problem that I want to insert and delete some chars in the middle of a file. fopen() and fdopen() just allow to append at the end. Is there any simple method or existing library that allow these actions? Thanks in advance.:confused: (7 Replies)
Discussion started by: ivancheung
7 Replies

2. Shell Programming and Scripting

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 (1 Reply)
Discussion started by: mopimp
1 Replies

3. Shell Programming and Scripting

How to insert text into first line of the file and middle of the file?

Script 1 Pre-requisites Create a file with x amount of lines in it, the content of your choice. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the... (3 Replies)
Discussion started by: ali hussain
3 Replies

4. Shell Programming and Scripting

How to insert the 1st arg into the middle of the file

I wrote a script like #!/bin/bash echo $1 > temp cat $2 >> temp mv temp $2 now I have problem appending the above script(only using bash shell) so that it now inserts the first argument into the middle of the file. I have tried using $(('wc -l < file' / 2 )) but invain so could any one... (4 Replies)
Discussion started by: boris
4 Replies

5. Shell Programming and Scripting

insert one file into middle of another file

how to insert one file into another file not by concatenating as usual done. file1 A B C D E F G H I J K L file2 23455 33444 33334 33345 Output shud be 23455 A B C D (4 Replies)
Discussion started by: cdfd123
4 Replies

6. Shell Programming and Scripting

insert text in the middle of a file

I want to insert a text into the middle of a file (3 Replies)
Discussion started by: relle
3 Replies

7. Shell Programming and Scripting

add text in the middle of file

Can anyone help me pls? I want to add a text into the middle of file. I've writtenthe following script text to add="$1" file="$2" lines=$(wc -l $2) half_lines=$(expr $lines / 2) head -$half_lines $2 > temp echo "text to add" >> temp ((half_lines=$half_lines + 1)) tail -$half_lines $2... (6 Replies)
Discussion started by: relle
6 Replies

8. Shell Programming and Scripting

How do I insert a line in the middle of a file in BASH?

I have tried sed '/6/a text_to_inserted' file > newfile but this inserts test_to_insert at random places in file and i want it in specific location, which is line 6. can anyone help.... (6 Replies)
Discussion started by: phenom
6 Replies

9. Shell Programming and Scripting

Adding Text To Middle Of File

Hi I am trying to write a bash script to add a line of text to the middle of a file. The way I worked it out was to calculate the number of lines and divide by 2. The file I am using is called newfile and it just contains lines of data. The new file I have created I have called midfile and... (7 Replies)
Discussion started by: BundBash
7 Replies

10. Shell Programming and Scripting

How to insert text in the middle of a file?

Hi, So far i've made a script that takes two argument, 1st is the contents and the 2nd is the named file. At the moment i've managed to insert new contents as a new line at the top, but i want to ask how can you insert contents in the middle of the file? Source Code #!/bin/bash #Write... (3 Replies)
Discussion started by: zen10
3 Replies
tmpfile(3C)						   Standard C Library Functions 					       tmpfile(3C)

NAME
tmpfile - create a temporary file SYNOPSIS
#include <stdio.h> FILE *tmpfile(void); DESCRIPTION
The tmpfile() function creates a temporary file and opens a corresponding stream. The file will automatically be deleted when all refer- ences to the file are closed. The file is opened as in fopen(3C) for update (w+). The largest value that can be represented correctly in an object of type off_t will be established as the offset maximum in the open file description. RETURN VALUES
Upon successful completion, tmpfile() returns a pointer to the stream of the file that is created. Otherwise, it returns a null pointer and sets errno to indicate the error. ERRORS
The tmpfile() function will fail if: EINTR A signal was caught during the execution of tmpfile(). EMFILE There are OPEN_MAX file descriptors currently open in the calling process. ENFILE The maximum allowable number of files is currently open in the system. ENOSPC The directory or file system which would contain the new file cannot be expanded. The tmpfile() function may fail if: EMFILE There are FOPEN_MAX streams currently open in the calling process. ENOMEM Insufficient storage space is available. USAGE
The stream refers to a file which is unlinked. If the process is killed in the period between file creation and unlinking, a permanent file may be left behind. The tmpfile() function has a transitional interface for 64-bit file offsets. See lf64(5). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
unlink(2), fopen(3C), mkstemp(3C), mktemp(3C), tmpnam(3C), lf64(5), standards(5) SunOS 5.10 4 Aug 2003 tmpfile(3C)
All times are GMT -4. The time now is 03:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy