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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-02-2011, 02:32 PM
|
#1
|
|
Member
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 107
Rep:
|
Find Text After String Search in Text File
Hello, spent too much time searching this and I know it's as simple as pie. I have a file called Regions.ini that looks like this:
Code:
[Granite]
RegionUUID = 54ab7cd2-0e70-49b7-8020-8dbeb84c08d0
Location = 9991,10007
InternalAddress = 0.0.0.0
InternalPort = 9001
AllowAlternatePorts = False
ExternalHostName = 71.171.21.9
[Syenite]
RegionUUID = 8fc56fdd-0afd-4074-9432-0ae8f42b799f
Location = 9992,10007
InternalAddress = 0.0.0.0
InternalPort = 9000
AllowAlternatePorts = False
ExternalHostName = 71.171.21.9
What I need to do is find out what the IP address is after "ExternalHostName ="
Thats it. Pretty simple huh.
After that I will need to compare that IP to whatismyip and if it's different then replace it but that is easy to do with sed. I just can't figure this simple hurdle out.
Thanks.
|
|
|
|
08-02-2011, 02:51 PM
|
#2
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Hi,
try this to get the IPs
Code:
awk '{ if ($1 == "ExternalHostName") { print $3 } }' file
|
|
|
|
08-02-2011, 03:23 PM
|
#3
|
|
Member
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 107
Original Poster
Rep:
|
Quote:
Originally Posted by crts
Hi,
try this to get the IPs
Code:
awk '{ if ($1 == "ExternalHostName") { print $3 } }' file
|
Yes thanks a lot! here is the output:
71.171.21.9
71.171.21.9
How do I get it to just print the IP once?
|
|
|
|
08-02-2011, 03:33 PM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,089
|
You can use 'uniq' if you want to get rid of duplicate lines.
|
|
|
|
08-02-2011, 03:41 PM
|
#5
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,089
|
A sed version of the same would be:
Code:
sed -n '/ExternalHostName = / s///p' file | uniq
|
|
|
|
08-02-2011, 03:42 PM
|
#6
|
|
Member
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 107
Original Poster
Rep:
|
Perfect! You guys are great.
crts would you be so kind as to explain how that command works? I'd like to learn something other then cut and paste 
|
|
|
|
08-02-2011, 03:43 PM
|
#7
|
|
Member
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 107
Original Poster
Rep:
|
Ah the sed statement works also thank you.
|
|
|
|
08-02-2011, 03:44 PM
|
#8
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Quote:
Originally Posted by redir
Yes thanks a lot! here is the output:
71.171.21.9
71.171.21.9
How do I get it to just print the IP once?
|
That depends. Are there many differenr IP that you need to extract? Then use the aforementioned 'uniq'. If this is the only IP-pair in your file then you could simply 'exit' the awk script after the first encounter:
Code:
awk '{ if ($1 == "ExternalHostName") { print $3;exit } }' file
|
|
|
|
08-02-2011, 03:47 PM
|
#9
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,089
|
Knowing that it's always going to be line 7 would make 'uniq' unnecessary:
Code:
sed -n '7s/ExternalHostName = //p' file
|
|
|
|
08-02-2011, 03:50 PM
|
#10
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,089
|
As crts mentioned there are a lot of factors that depend on your input file(s). Is the file you're working with exactly like the one you posted or is it just a sample of a longer file with the same/different IP addresses?
Here's a great tutorial on both awk and sed:
http://www.grymoire.com/Unix/
Last edited by sycamorex; 08-02-2011 at 03:52 PM.
|
|
|
|
08-02-2011, 03:52 PM
|
#11
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Quote:
Originally Posted by redir
Perfect! You guys are great.
crts would you be so kind as to explain how that command works? I'd like to learn something other then cut and paste 
|
Ok, let's break it down.
Code:
awk '{ if (condition) { commands } }' file
The commands inside '{}' will only be executed if (condition) is true.
In out case the condition is:
Code:
$1 == "ExternalHostName"
The $1 refers in awk to the first column. It is not to be confused with bash's positional parameters. So our condition checks every line of the input, if the first word is ExternalHostName. If that is true then
is executed. This instructs awk to print the third word of this line.
For further reference you might want to read:
http://www.gnu.org/software/gawk/manual/gawk.html
|
|
|
|
08-02-2011, 03:53 PM
|
#12
|
|
Member
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 107
Original Poster
Rep:
|
Yeah that file is exactly like that and will never change. I have dynamic dns so when my ISP changes my IP address I need to open that file, change to the new IP then close it and restart the simulator program.
|
|
|
|
08-02-2011, 03:57 PM
|
#13
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,089
|
Quote:
Originally Posted by redir
Yeah that file is exactly like that and will never change. I have dynamic dns so when my ISP changes my IP address I need to open that file, change to the new IP then close it and restart the simulator program.
|
Yeah, in that case any solution that has been provided in this thread should work just fine. If you want to learn more about awk/sed, please have a look at the tutorial I linked to in one of my previous posts. It's very good.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|