Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
10.1.1.2:10026 inet n - n - - smtpd
I wanted to replace text before :10025 and :10026 with a different IP address. e.g. 10.2.2.5 so the final output in the file is
Code:
10.2.2.5:10025 inet n - n - - smtpd
10.2.2.5:10026 inet n - n - - smtpd
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...
Last edited by ardvark71; 04-01-2016 at 04:35 PM.
Reason: Added information and link.
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
The code above will not make changes to your original file. You're just modifying the output of sed against the file. This is like a preview to check if the results are what you want.
If the code produces the desired output with the new IP change, run the code again with -i to sed to overwrite /etc/hosts.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.