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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
insert multiple lines into a file c0mrade Shell Programming and Scripting 12 09-04-2008 04:04 PM
Insert 2 lines in a file at a specific location potro Shell Programming and Scripting 2 05-08-2008 07:38 PM
Remove duplicates from File from specific location gopikgunda Shell Programming and Scripting 1 04-09-2008 02:16 AM
insert file 1 at a specific place of file 2 JCR Shell Programming and Scripting 1 02-01-2007 11:48 AM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-23-2005 12:04 AM

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

Join Date: Mar 2002
Posts: 272
Insert lines at specific location in file

Hi There

I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket)

Now, I know how to do this by grep'ing tailing, appending etc using temporary files to put stuff in, but I wanted to do it in one simple command rather than 5 or 6 commands with other temp files involved that I have to remove afterwards (it looks very messy)

Is there any way to do this simply ????


Code:
stuff
stuff
stuff
<NEED TO ENTER LINE HERE>
}
  #2 (permalink)  
Old 03-28-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Code:
ed file<<!
-1a
my new line
.
w
q
!

Last edited by radoulov; 03-28-2007 at 05:48 AM..
  #3 (permalink)  
Old 03-28-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
Try this...

Code:
sed '$ i this is  line you want to add' filename
Output:

u142115@linux2alm:~/aps/aps4/product/den> cat sc.txt
stuff
stuff
stuff
}
u142115@linux2alm:~/aps/aps4/product/den> sed '$ i this is the line' sc.txt
stuff
stuff
stuff
this is the line
}
  #4 (permalink)  
Old 03-28-2007
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
Quote:
Originally Posted by jacoden
Try this...

Code:
sed '$ i this is  line you want to add' filename
Output:

u142115@linux2alm:~/aps/aps4/product/den> cat sc.txt
stuff
stuff
stuff
}
u142115@linux2alm:~/aps/aps4/product/den> sed '$ i this is the line' sc.txt
stuff
stuff
stuff
this is the line
}

when I run this I get

Code:
[my.server] # sed '$ i this is the line' testfile
sed: command garbled: $ i this is the line
[my.server] #
  #5 (permalink)  
Old 03-28-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
Give an attempt on this... All works well here...

Code:
x="this is the line"
sed '$ i '"$x"'' file
  #6 (permalink)  
Old 03-28-2007
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
must be a different version of sed unfortunatley, cos it doesnt work for me
  #7 (permalink)  
Old 03-28-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by hcclnoodles
when I run this I get

Code:
[my.server] # sed '$ i this is the line' testfile
sed: command garbled: $ i this is the line
[my.server] #

For older versions of sed:

Code:
sed '$i\
this is  line you want to add' file
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 11:21 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