LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-01-2016, 03:28 PM   #1
mathewrond
LQ Newbie
 
Registered: Mar 2016
Posts: 6

Rep: Reputation: Disabled
Question 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
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
Can anyone help with this?
 
Old 04-01-2016, 04:26 PM   #2
ardvark71
LQ Veteran
 
Registered: Feb 2015
Location: USA
Distribution: Lubuntu 14.04, 22.04, Windows 8.1 and 10
Posts: 6,282
Blog Entries: 4

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842
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...

Last edited by ardvark71; 04-01-2016 at 04:35 PM. Reason: Added information and link.
 
Old 04-01-2016, 05:28 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
sed would also be a good choice.
 
Old 04-01-2016, 08:52 PM   #4
WayneB
LQ Newbie
 
Registered: Mar 2016
Posts: 25

Rep: Reputation: Disabled
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.

Code:
sed -i 's/10.1.1.2/10.2.2.5/g' /etc/hosts

Last edited by WayneB; 04-01-2016 at 08:54 PM.
 
Old 04-01-2016, 09:44 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,324
Blog Entries: 28

Rep: Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142
In case it helps, Dave Morriss is doing a series on sed at Hacker Public Radio.
 
Old 04-02-2016, 05:35 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Regarding the sed solution, the following might be closer to your requirement
Code:
sed 's/[^:]*\(:1002[56]\)/10.1.1.2\1/' file
I.e. restore a part of the search pattern by means of \( \) and its reference \1
 
Old 04-02-2016, 05:45 PM   #7
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
open the file in gedit
and click on edit/ find and replace
 
Old 04-02-2016, 06:25 PM   #8
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,628

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
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.
 
Old 04-03-2016, 10:04 PM   #9
mathewrond
LQ Newbie
 
Registered: Mar 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you for your suggestion, MadeInGermany. This is exactly what I was looking for.
 
Old 04-03-2016, 11:20 PM   #10
ardvark71
LQ Veteran
 
Registered: Feb 2015
Location: USA
Distribution: Lubuntu 14.04, 22.04, Windows 8.1 and 10
Posts: 6,282
Blog Entries: 4

Rep: Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842Reputation: 842
Quote:
Originally Posted by mathewrond View Post
Thank you for your suggestion, MadeInGermany. This is exactly what I was looking for.
Glad you got it resolved.

Regards...
 
  


Reply



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
[SOLVED] Find & replace text from file 1 with text from file 2? sttumped Linux - Newbie 5 11-02-2012 03:58 PM
[SOLVED] command to find/replace in a text file? sneakyimp Linux - Newbie 17 02-15-2011 12:30 AM
Linux command to find and replace string within text file chips11 Linux - Newbie 5 11-24-2008 02:25 PM
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:00 AM.

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