LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash: replace a line in text file (http://www.linuxquestions.org/questions/programming-9/bash-replace-a-line-in-text-file-631208/)

freeindy 03-28-2008 05:30 AM

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

exscape 03-28-2008 05:37 AM

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'

pixellany 03-28-2008 06:01 AM

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

freeindy 03-28-2008 06:59 AM

thanks.

Works nicely exscape

Indy

jdaoutid 03-26-2010 11:23 AM

Quote:

Originally Posted by exscape (Post 3102926)
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?

exscape 03-26-2010 11:28 AM

Quote:

Originally Posted by jdaoutid (Post 3913420)
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


jdaoutid 03-26-2010 11:59 AM

Quote:

Originally Posted by exscape (Post 3913427)
Try double quotes:
Code:

$ perl -pe "s/.*/$new_line/ if $. == 2" < test

Brilliant!!! Thanx a lot.

droidzone 04-23-2011 02:49 PM

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?

crts 04-23-2011 04:20 PM

Quote:

Originally Posted by droidzone (Post 4333823)
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]

kmkalai 09-08-2011 09:27 AM

Hi,

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

Thanks,
Kalai.

Proud 09-08-2011 12:08 PM

Quote:

Originally Posted by exscape (Post 3102926)
Or, in perl:
perl -pe 's/.*/new test/ if $. == 2'

Please don't res. old threads without reading them first.


All times are GMT -5. The time now is 09:43 PM.