LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-21-2009, 12:20 PM   #1
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Rep: Reputation: 15
replace only the first line of a file without temporary files


hello I have a file in which I want to replace the first line with the context of another file. I have done this with temporary files, with I want to know if there is an easier and more efficient way to do this with sed, awk or other stream editor.

I have a main file whose content is:
header

text...
...
text...

I have a file that has the text I want to insert in the fist line.
The output I want is

newHeader

text...
...
text...

How can I do this without using temporary files? Thanks
 
Old 04-21-2009, 01:33 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

As always there are probably others ways of doing this, but this should work:

- A sed solution -

Code:
#!/bin/bash
sed -i '/header/ {
  r newheader
  d
}' $1
Sed searches for header (the /header/ part) and if found replace it with the content of the file called newheader (the r newheader and d part). The -i saves changes immediately.


Files used and testrun:

Quote:
cat mainfile

a one line header

Text text .......
Text text .......


Text text .......

Text text .......


cat newheader
This is

--- The New Header ---

that's all folks


./replaceheader mainfile

This is

--- The New Header ---

that's all folks

Text text .......
Text text .......


Text text .......

Text text .......
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to replace a line in a file with another line. another 1 Linux - Newbie 5 04-11-2009 08:09 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
Command Line File Replace MissingLink Linux - Newbie 3 02-06-2009 02:47 AM
Replace line in file with two lines?! Possible? eur0dad Linux - General 2 09-07-2006 02:15 PM
find and replace in files from command line dexter_modem Linux - General 4 06-10-2003 11:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration