Sponsored Content
Top Forums Shell Programming and Scripting Adding Text To Middle Of File Post 302387985 by BundBash on Tuesday 19th of January 2010 04:52:34 AM
Old 01-19-2010
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 the line of text which will be added is called myline.

code I have done so far is below:
Code:
#!/bin/bash
 
num=$(((wc -l $newfile)/2))
 
cat newfile | head -$num > midfile
cat myline >> midfile
cat newfile | tail -$num >> midfile

At the moment the new file is created but it only contains the line which is supposed to go in the middle. Im guessing the mistake I have made is simple but can't seem to figure out where I have gone wrong. Many Thanks in Advance.

Last edited by Scott; 01-19-2010 at 06:29 AM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

How to insert text in the middle of a file

Hey guys, how do we take a line of text as an argument from a user and then insert it in the middle of a file irrespective of the number of lines in the file. I am trying to do this without SED or AWK. Inserting it in the beginning and at the end is easy, but i am trying to accomplish inserting... (6 Replies)
Discussion started by: kartikkumar84@g
6 Replies

6. Shell Programming and Scripting

Script to add a single line to middle of text file.

I've got a configuration file that is filled with xml text statements for example: <...../> <...../> <...../> <data id="java-options" value="-server -Djava.security.policy..../> <...../> <...../> <...../> I want to write a korn shell script that will go to this specific line and add a... (2 Replies)
Discussion started by: progkcp
2 Replies

7. Shell Programming and Scripting

How to read from middle of a file for a particular text

Hi, Below is my issue which I desperately need and I want a shell script which can do this job. 1. There are 10 log files in a particular location. 2. open each log file. Goto to the end of the file. From the end go up to find a particular text. From this particular text till the end of... (3 Replies)
Discussion started by: kashriram
3 Replies

8. 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

9. Shell Programming and Scripting

Unix File - Adding columns in the middle

Hello, I have a comma separated flat file. It contains some 20 columns. I want to add two new columns at position 2,3. So that file will have 22 columns. I am providing here sample data with file having 4 columns. Appreciate your help in finding solution for this. data in input file:... (11 Replies)
Discussion started by: ravi.videla
11 Replies

10. Shell Programming and Scripting

Adding new column in the middle

Hi , I need to add few new columns in existing file .Any help would be great ex: existing file name,typ,add,dept New file(com1,sal are new) name,com1,type,sal,add,dept Thanks, mohan (1 Reply)
Discussion started by: mohan705
1 Replies
diffmk(1)							   User Commands							 diffmk(1)

NAME
diffmk - mark differences between versions of a troff input file SYNOPSIS
diffmk oldfile newfile markedfile DESCRIPTION
diffmk compares two versions of a file and creates a third version that includes "change mark" (.mc) commands for nroff(1) and troff(1). oldfile and newfile are the old and new versions of the file. diffmk generates markedfile, which, contains the text from newfile with troff(1) "change mark" requests (.mc) inserted where newfile differs from oldfile. When markedfile is formatted, changed or inserted text is shown by | at the right margin of each line. The position of deleted text is shown by a single *. USAGE
See largefile(5) for the description of the behavior of diffmk when encountering files greater than or equal to 2 Gbyte ( 2**31 bytes). EXAMPLES
Example 1: An example of the diffmk command. diffmk can also be used in conjunction with the proper troff requests to produce program listings with marked changes. In the following command line: example% diffmk old.c new.c marked.c ; nroff reqs marked.c | pr the file reqs contains the following troff requests: .pl 1 .ll 77 .nf .eo .nh which eliminate page breaks, adjust the line length, set no-fill mode, ignore escape characters, and turn off hyphenation, respectively. If the characters | and * are inappropriate, you might run markedfile through sed(1) to globally change them. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWdoc | +-----------------------------+-----------------------------+ SEE ALSO
diff(1), nroff(1), sed(1), troff(1), attributes(5), largefile(5) BUGS
Aesthetic considerations may dictate manual adjustment of some output. File differences involving only formatting requests may produce undesirable output, that is, replacing .sp by .sp 2 will produce a "change mark" on the preceding or following line of output. SunOS 5.10 14 Sep 1992 diffmk(1)
All times are GMT -4. The time now is 06:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy