LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 06:30 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Rep: Reputation: 30
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, 06: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 06:38 AM.
 
Old 03-28-2008, 07:01 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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, 07:59 AM   #4
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Original Poster
Rep: Reputation: 30
thanks.

Works nicely exscape

Indy
 
Old 03-26-2010, 12:23 PM   #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, 12:28 PM   #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, 12:59 PM   #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, 03: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, 05:20 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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, 10: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, 01: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


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


All times are GMT -5. The time now is 03:15 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration