The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inserting a column in a file dhanamurthy Shell Programming and Scripting 7 05-11-2008 07:29 AM
Inserting New Line in File using Sed ?? Mary_xxx Shell Programming and Scripting 4 02-27-2008 12:50 PM
inserting into a data file paul1s UNIX for Dummies Questions & Answers 4 10-12-2006 11:47 PM
Inserting a new column in a file rosh0623 UNIX for Advanced & Expert Users 4 08-01-2006 08:15 AM
inserting a String in the file(s) 2tbee Shell Programming and Scripting 4 04-11-2006 02:36 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-05-2006
Registered User
 

Join Date: Oct 2006
Posts: 4
Question Inserting argument into top of a file

Hi,

I am new to Unix, and I am trying to append a line of argument into a current file. I need this line to be inserted into the very top of the file. Does anyone know how this is done?

For example, I am trying: echo "insert to top" >> filename. This inserts the line at the bottom of the file. How do I get it to insert to the top??

Please help!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-05-2006
Glenn Arndt's Avatar
Anomalous Lurker
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Code:
{
  echo "This is the first line."
  cat originalFile
} > newFile
mv newFile originalFile
Reply With Quote
  #3 (permalink)  
Old 10-05-2006
Registered User
 

Join Date: Oct 2006
Posts: 4
Hi Glenn,

Thank you for your input. I cannot do the way you are showing me. My task is to input a line to an existing file without creating new or temporary file.

That is why I have used the >> symbol, but this inserts the line at the bottom. Do you know how to insert to the top without creating extra files?
Reply With Quote
  #4 (permalink)  
Old 10-05-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Code:
/bin/echo '1i\nNew Line Goes Here\n.\nwq' | ex -s myFile
Reply With Quote
  #5 (permalink)  
Old 10-05-2006
Glenn Arndt's Avatar
Anomalous Lurker
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
Whenever I see "without creating new or temporary file", I think "homework". You can use perl to edit files in place. Search the forums -- you will find many examples of this.
Reply With Quote
  #6 (permalink)  
Old 10-05-2006
Registered User
 

Join Date: Oct 2006
Posts: 4
Thanks for the input vgersh99. But again, that line of code inserts the line at the end of the file.. I am trying to get the line to be inserted at the top.

And Glenn, I understand where you are coming from, but I just don't wont to have so many temporary files created for no reason.

Any ideas??
Reply With Quote
  #7 (permalink)  
Old 10-05-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Code:
printf '1i\nNew Line Goes Here\n.\nwq!\n' | ex myFile
OR [in-line]

Code:
ex myFile <<EOF
1i
New Line Goes Here
.
wq
EOF
either one of the above inserts a line as the TOP line in a file.

Last edited by vgersh99; 10-05-2006 at 10:29 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:05 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