The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2008
coolkid coolkid is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 69
sed

Guys
do you know any other command that will insert a line at particular line number...I used this one...but it is not working if the above lines are blank.

This command will put "name=cat.abc.com at 11th line.But if there is no line 10 the command is not working...

sed '10a\ name = cat.abc.com' con.txt


Really appreciate if you guys can help me out with this...alll i need is a command to put lines (append or insert) at particular line number...

Thanks alot
CK
  #2 (permalink)  
Old 08-28-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,086
you may pass a var into SED to indicate the line number

sed $line'a\
insert line
' filename
  #3 (permalink)  
Old 08-29-2008
coolkid coolkid is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 69
Summer
tried ur solution like this...


line=10
sed $line'a\ insert_line con.txt

But getting some errors...
sed: can't read line: No such file or directory

Any suggestions...
  #4 (permalink)  
Old 08-29-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,421
Code:
awk 'NR==11{print "new line"}1'  file

Last edited by danmero; 08-29-2008 at 03:28 PM..
  #5 (permalink)  
Old 08-29-2008
coolkid coolkid is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 69
danmero
Thanks for the code man..its working...but only at the prompt..its not really changing the file...so

I tried awk 'NR==11{print "new line"}1' file>file2.

But using "file2" is not a feasible solution for me..The change has to happen in "file" itself.

do u know a solution that doesnt use file2.Please let me know.



--Cal
  #6 (permalink)  
Old 08-29-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,421
You should check the man page for your sed:
Code:
sed -i '11 i\
name=cat.abc.com' file
  #7 (permalink)  
Old 08-30-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,391
if file has less than 10 lines i don't think you can add 11th line to it..
you can try this to add text above any line you want
Quote:
vi - filename <<!
:10(anylineno)
:r !echo "the line you wanna enter"
:wq
!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0