LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   My sed/gawk script works, but is complicated. How would you do this? (https://www.linuxquestions.org/questions/linux-newbie-8/my-sed-gawk-script-works-but-is-complicated-how-would-you-do-this-897873/)

809areacode 08-17-2011 11:24 AM

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. :)

macemoneta 08-17-2011 12:05 PM

How about:
Code:

ifconfig | awk '/inet addr:/ {print substr($2,6);exit}'

sycamorex 08-17-2011 12:53 PM

Quote:

Originally Posted by macemoneta (Post 4445740)
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

809areacode 08-17-2011 01:10 PM

I guess you could grep wlan0 or whatever your main wireless device is before awk?

sycamorex 08-17-2011 01:18 PM

Quote:

Originally Posted by 809areacode (Post 4445805)
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:)


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