LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-28-2008, 05:30 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
bash: replace a line in text file


Hi,

I want to change a line (for example line 3) in a text file.

so: a text file that has text

Code:
a text file
line to be replaced!
just some more text
and after manipulation

Code:
a text file
Text replaced.
just some more text
any ideas?

Indy
 
Old 03-28-2008, 05:37 AM   #2
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
sed '2s/.*/new contents/' seems to work. The 2 of course means line 2

Edit: Or, in perl:
perl -pe 's/.*/new test/ if $. == 2'

Last edited by exscape; 03-28-2008 at 05:38 AM.
 
Old 03-28-2008, 06:01 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
freeindy;
You don't state the criteria for the change. e.g. based on line #?...content?

Here is my favorite SED tutorial. The same site has many other, including AWK.

http://www.grymoire.com/Unix/Sed.html#uh-8
 
Old 03-28-2008, 06:59 AM   #4
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
thanks.

Works nicely exscape

Indy
 
Old 03-26-2010, 11:23 AM   #5
jdaoutid
LQ Newbie
 
Registered: Mar 2010
Location: Brussels
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by exscape View Post
sed '2s/.*/new contents/' seems to work. The 2 of course means line 2

Edit: Or, in perl:
perl -pe 's/.*/new test/ if $. == 2'
Thats fine, but what if I have an argument referring to this new line, E.g.
Code:
new_line = "line to be replaced!"
perl -pe 's/.*/$new_line/ if $. == 2' <old>new
Unfortunately, this does not work. Instead, it makes the second line blank. Where is my mistake here?
 
Old 03-26-2010, 11:28 AM   #6
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
Quote:
Originally Posted by jdaoutid View Post
Thats fine, but what if I have an argument referring to this new line, E.g.
Code:
new_line = "line to be replaced!"
perl -pe 's/.*/$new_line/ if $. == 2' <old>new
Unfortunately, this does not work. Instead, it makes the second line blank. Where is my mistake here?
The problem is that you set the bash variable $new_line, but use single quotes, so perl sees exactly what you've written, and replaces it with the contents of the perl variable $new_line, which is undefined.
Try double quotes:
Code:
$ new_line="new line 2"

$ cat > test
line 1
line 2
line 3

$ perl -pe "s/.*/$new_line/ if $. == 2" < test
line 1
new line 2
line 3
 
Old 03-26-2010, 11:59 AM   #7
jdaoutid
LQ Newbie
 
Registered: Mar 2010
Location: Brussels
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by exscape View Post
Try double quotes:
Code:
$ perl -pe "s/.*/$new_line/ if $. == 2" < test
Brilliant!!! Thanx a lot.
 
Old 04-23-2011, 02:49 PM   #8
droidzone
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
Similiar qn

Hi..
I'd like to do the same thing with sed. Could you tell me how to?

My File:

rom.devid=dz
rom.version=sb16668
dsdversion=oioio
etc
etc

I wish to search for a line containing "rom.version" and replace it with:
ro.version=newrom899

Could you tell me how to do this with sed/grep and write the contents to a new file?
 
Old 04-23-2011, 04:20 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by droidzone View Post
Hi..
I'd like to do the same thing with sed. Could you tell me how to?

My File:

rom.devid=dz
rom.version=sb16668
dsdversion=oioio
etc
etc

I wish to search for a line containing "rom.version" and replace it with:
ro.version=newrom899

Could you tell me how to do this with sed/grep and write the contents to a new file?
Hi and welcome to LQ,

in the future please start a new thread and maybe link to the thread that describes a similar problem.

As for your question, is this what you are looking for?
Code:
sed 's/^rom\.version.*/ro.version=newrom899/' file > newfile
[UNTESTED]
 
Old 09-08-2011, 09:27 AM   #10
kmkalai
LQ Newbie
 
Registered: Sep 2011
Posts: 1

Rep: Reputation: Disabled
Hi,

This line works fine in command line. Please let me know how to use this in a perl program.

Thanks,
Kalai.
 
Old 09-08-2011, 12:08 PM   #11
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by exscape View Post
Or, in perl:
perl -pe 's/.*/new test/ if $. == 2'
Please don't res. old threads without reading them first.
 
  


Reply

Tags
bash, perl, replacement, sed



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 string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 PM.

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