The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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 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
insert text into the middle of a original file mopimp Shell Programming and Scripting 1 03-26-2006 07:11 AM
how to insert and delete characters in the middle of file ivancheung High Level Programming 7 10-11-2004 12:08 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-11-2007
Registered User
 

Join Date: Apr 2007
Posts: 6
Angry

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 help.
Reply With Quote

Last edited by boris; 04-13-2007 at 11:43 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-11-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,573
something like this,

to be in the middle
Code:
a=$((`wc -l < filename` / 2))
awk -v var=$a '{ if( NR == var) { print "newline to be added"; print } else { print } }' filename > newf
mv newf filename
Reply With Quote
  #3 (permalink)  
Old 04-11-2007
dj -------
 

Join Date: Feb 2007
Location: Cochin/Bangalore, India
Posts: 380
Try this

Code:
a=$((`wc -l < filename` / 2))
sed ''$a' a this is new line' filename
Reply With Quote
  #4 (permalink)  
Old 04-12-2007
Registered User
 

Join Date: Apr 2007
Posts: 6
when I insert this command and run the sript it give a message stating Syntax error.....is there any other command like head or tail command





Quote:
Originally Posted by jacoden
Try this

Code:
a=$((`wc -l < filename` / 2))
sed ''$a' a this is new line' filename
Reply With Quote
  #5 (permalink)  
Old 04-12-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by boris
when I insert this command and run the sript it give a message stating Syntax error.....is there any other command like head or tail command
Code:
sed "${a}a\\
this is new line" filename
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0