Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-31-2008, 12:58 PM   #1
LQ Newbie
Registered: Jun 2008
Posts: 26

Rep: Reputation: 15
using sed to insert line into file and overwrite the current file

hey there all

so what i need to do is insert a line (or couple of lines) into an existing file at a certain line number (say line 4).... sed by default write things to the standard output or to a file, i was hoping there was a way to overwrite the existing file... i dont want to write to some temporary file, then copy the temp file over the "live file" and then delete the temp file

so lets say my current file is called livefile.txt, i know i could do something like the following (i havent checked whether this works or not, but you'll get my meaning)

sed '4a\Line to insert' livefile.txt > templivefile.txt

then overwrite the live file with the updated tempfile, but i dont want to do this last step... i would like to simply change the livefile.txt without having to output to a temp file... is this even possible with sed?

any help would be greatly appreciated
Old 10-31-2008, 01:19 PM   #2
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46

if you're working with GNU sed, there's the -i option available ("inplace").

Old 10-31-2008, 01:32 PM   #3
LQ Newbie
Registered: Jun 2008
Posts: 26

Original Poster
Rep: Reputation: 15
sweet! that works perfectly... much appreciated
Old 06-11-2009, 07:14 PM   #4
Registered: May 2007
Posts: 35

Rep: Reputation: 15
Hi ,

I want to read from the file and check for the pattern, if the line has some word like <string>: then string should be copied into buffer.

Afterwards, I want to insert the same <string> with some word in the next line of the file.

Could you please help me out to use sed command to perform the above mentioned operations?



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 overwrite a particular line of a file kkpal Linux - Newbie 1 05-16-2008 02:35 AM
[SOLVED] use awk &/or sed to read file 1 line 1 & file 2 line 1 gr8scot Linux - General 6 07-12-2007 08:13 PM
insert a single quote is text file with sed eln01 Programming 7 05-10-2007 11:02 AM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:43 AM.

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