add text in the middle of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add text in the middle of file
# 1  
Old 03-17-2008
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 >> temp
mv temp $2

it gives me `expr : syntax error

Can someone tell me what i did wrong. Was i not to use expr command?
# 2  
Old 03-17-2008
Try the wc command manually, it gives the number of lines and as second field the filename.
That's why you get an error with the expr command.

Regards
# 3  
Old 03-17-2008
Try this script

[[ -z $1 ]]&& echo "Argument needed "&& exit
[[ ! -s $1 ]]&& echo "File not found "&& exit
rm -f $1New
var=`wc -l <$1`
echo "Enter line no :\c"
read lineno
echo "Enter Text:\c"
read text
head -$lineno $1 >$1New
echo "$text" >>$1New
var1=$((var-lineno))
tail -$var1 $1 >>$1New
# 4  
Old 03-17-2008
Hi Franklin
I have tried this but then i get an error command not found in line 5
# 5  
Old 03-17-2008
Quote:
Originally Posted by relle
Hi Franklin
I have tried this but then i get an error command not found in line 5
Do you react on my response or on sanjaypraj reponse?

Regards
# 6  
Old 03-19-2008
input:
Code:
first
second
forth
fifth

output:
Code:
first
second
third
forth
fifth

Code:
line=`cat a | wc -l`
half=`expr $line / 2`
nawk -v s="$half" '{
if(NR<=s) 
print
}' a
echo "third"
nawk -v s="$half" '{
if(NR>s) 
print
}' filename

# 7  
Old 03-20-2008
How abt this

Code:
$cat test
first
second
fourth
fifth

$cat  test | awk '{print $0}(NR==2){print "third"}'
first
second
third
fourth
fifth

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

4. UNIX for Dummies Questions & Answers

Add string to middle of a file

Hi, I want to write a script that takes a file and a string as params and adds the string to the middle line of the file. Also, I want to output the results back to the original file passed without using temp files. I am very much new to UNIX so this is all a little like black magic to me at... (15 Replies)
Discussion started by: Chiefos
15 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

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

8. Shell Programming and Scripting

add a string in the middle of the file

i want to add a string in a very top of a file without using VI or SED or AWK this is what ive done: (echo '0a'; echo 'LINE OF TEXT'; echo '.'; echo 'wq') | ed -s myfile to add astrng right in the middle i could have count the lines of the file and just chenge the address. ... (6 Replies)
Discussion started by: ciroredz
6 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

Featured Tech Videos