LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-17-2011, 12:24 PM   #1
809areacode
LQ Newbie
 
Registered: Aug 2011
Location: Godthåb, Grønland
Distribution: Debian
Posts: 28

Rep: Reputation: Disabled
Post My sed/gawk script works, but is complicated. How would you do this?


I'm using /sbin/ifconfig and piping it into sed and gawk to retrieve just my system's IP address. I placed the script in a file called 'ifi', which I run whenever I need an IP address in another script. It works fine.

Code:
$ echo `ifconfig | grep "inet addr:" |gawk -F: '{print $2}'` |sed '2p'|gawk '{print $3}'
192.168.x.x
$
Like I said, it works fine. Now I use it for logging...

Just... it's kind of complicated, don't you think? Just let me know how you would do it.
 
Old 08-17-2011, 01:05 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
How about:
Code:
ifconfig | awk '/inet addr:/ {print substr($2,6);exit}'
 
Old 08-17-2011, 01:53 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by macemoneta View Post
How about:
Code:
ifconfig | awk '/inet addr:/ {print substr($2,6);exit}'
It might also be a good idea to specify the network device (eg. ifconfig eth0 or ifconfig wlan0) because on my system it would return 127.0.0.1
 
Old 08-17-2011, 02:10 PM   #4
809areacode
LQ Newbie
 
Registered: Aug 2011
Location: Godthåb, Grønland
Distribution: Debian
Posts: 28

Original Poster
Rep: Reputation: Disabled
I guess you could grep wlan0 or whatever your main wireless device is before awk?
 
Old 08-17-2011, 02:18 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by 809areacode View Post
I guess you could grep wlan0 or whatever your main wireless device is before awk?
There's no need to grep anything, just specify the device:
Code:
ifconfig wlan0 | awk '/inet addr:/ {print substr($2,6);exit}'
I was just giving an example showing that macemoneta's solution (while correct) wouldn't work as intended on all systems.
Having said that, the solution works for you, which is the purpose of this thread

Last edited by sycamorex; 08-17-2011 at 02:20 PM.
 
  


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
awk, sed gawk - the hardest unix problem. webs Linux - Newbie 11 11-12-2009 11:08 AM
complicated pattern matching with awk or sed... alirezan1 Linux - Newbie 1 10-10-2008 07:45 PM
[sed || gawk]: find and delete blocks and lines from file Hisu Programming 1 09-16-2008 03:01 PM
SED script almosts works but with too many newlines.. cannontrodder Linux - General 2 01-16-2008 09:00 AM
Bit complicated sed search & replace sharathkv25 Programming 6 04-07-2007 02:53 PM


All times are GMT -5. The time now is 10:30 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration