LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 08-02-2011, 02:32 PM   #1
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 108

Rep: Reputation: 15
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.
 
Old 08-02-2011, 02:51 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

try this to get the IPs
Code:
awk '{ if ($1 == "ExternalHostName") { print $3 } }' file
 
Old 08-02-2011, 03:23 PM   #3
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 108

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by crts View Post
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?
 
Old 08-02-2011, 03:33 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,524
Blog Entries: 1

Rep: Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997
You can use 'uniq' if you want to get rid of duplicate lines.
 
Old 08-02-2011, 03:41 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,524
Blog Entries: 1

Rep: Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997
A sed version of the same would be:
Code:
sed -n '/ExternalHostName = / s///p' file | uniq
 
Old 08-02-2011, 03:42 PM   #6
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 108

Original Poster
Rep: Reputation: 15
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
 
Old 08-02-2011, 03:43 PM   #7
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 108

Original Poster
Rep: Reputation: 15
Ah the sed statement works also thank you.
 
Old 08-02-2011, 03:44 PM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by redir View Post
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
 
Old 08-02-2011, 03:47 PM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,524
Blog Entries: 1

Rep: Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997
Knowing that it's always going to be line 7 would make 'uniq' unnecessary:

Code:
sed -n '7s/ExternalHostName = //p' file
 
Old 08-02-2011, 03:50 PM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,524
Blog Entries: 1

Rep: Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997
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.
 
Old 08-02-2011, 03:52 PM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by redir View Post
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
Code:
print $3
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
 
Old 08-02-2011, 03:53 PM   #12
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 108

Original Poster
Rep: Reputation: 15
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.
 
Old 08-02-2011, 03:57 PM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,524
Blog Entries: 1

Rep: Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997
Quote:
Originally Posted by redir View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
PHP: Find a string in a text file and add something to that line aocferreira Programming 1 05-26-2011 01:52 PM
String search in text file using Visual C++....help needed usmanalam Programming 2 01-26-2010 01:05 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
Linux command to find and replace string within text file chips11 Linux - Newbie 5 11-24-2008 02:25 PM
How to search for a string in a text file using wildcards(*,?) tnjones Programming 2 08-23-2008 07:04 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration