Find and replace text in file
I have a file with an IP address that I wanted to change with a different IP address. I am using RedHat Linux, could you please let me know how I can do this?
Code:
10.1.1.2:10025 inet n - n - - smtpd Code:
10.2.2.5:10025 inet n - n - - smtpd |
Hi...
I'm guessing this is from a regular text file? If so, simply gain root access (or the root account,) if you don't already have it and use gedit (or another text editor) to open and change the file information as you have shown. :) One way of doing this is explained here but you could also just double click on the file in question. Regards... |
sed would also be a good choice.
|
If it's a simple search and replace, this is how it's done.
Code:
sed 's/10.1.1.2/10.2.2.5/g' /etc/hosts If the code produces the desired output with the new IP change, run the code again with -i to sed to overwrite /etc/hosts. Code:
sed -i 's/10.1.1.2/10.2.2.5/g' /etc/hosts |
In case it helps, Dave Morriss is doing a series on sed at Hacker Public Radio.
|
Regarding the sed solution, the following might be closer to your requirement
Code:
sed 's/[^:]*\(:1002[56]\)/10.1.1.2\1/' file |
open the file in gedit
and click on edit/ find and replace |
so MANY answers
sed can do this, perl can also, gsar is made for such things as well.
You have a simple pattern search and replace, twice. Easy. Man pages will help, but there are tons of how-to pages on these utilities. |
Thank you for your suggestion, MadeInGermany. This is exactly what I was looking for.
|
Quote:
Regards... |
All times are GMT -5. The time now is 03:55 AM. |