Adding Text To Middle Of File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding Text To Middle Of File
# 1  
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
# 2  
Old 01-19-2010
Quote:
num=$(((wc -l $newfile)/2))
check the output/error of this command. I think you will get syntex error.

try this:

Code:
num=$(expr $(wc -l < $newfile) / 2)

# 3  
Old 01-19-2010
Try:
Code:
num=$(($(wc -l<newfile)/2))

# 4  
Old 01-19-2010
Thanks Scrutinizer.....that is working great.....can u briefly explain why you add a < so i can understand......am a new starter Smilie
# 5  
Old 01-19-2010
Quote:
Originally Posted by BundBash
Thanks Scrutinizer.....that is working great.....can u briefly explain why you add a < so i can understand......am a new starter Smilie
Hi, if you use < then the wc command leaves out the file name.
# 6  
Old 01-19-2010
Quote:
Originally Posted by BundBash
Thanks Scrutinizer.....that is working great.....can u briefly explain why you add a < so i can understand......am a new starter Smilie
you can check by own.

Code:
/home->wc -l file
11 file
/home->wc -l < file
11
/home->

"<" is the indirection, which is sending file o/p to the command line by line.
as a result of that, you will get the line count only. ( unlikely the filename also which you get in the another command.)

if you use first command than you will have to suppress the filename part.
# 7  
Old 01-19-2010
Thanks, I tried that and it makes sense now.....

Was thinking....would separating the filename in brackets allow you to not include the filename??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question