The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Handling variable inputs yhacks High Level Programming 1 05-23-2008 07:04 AM
read n number of inputs er_aparna Shell Programming and Scripting 14 07-27-2006 12:35 PM
Validating inputs from a file sendhilmani123 Shell Programming and Scripting 1 05-10-2006 06:49 AM
Inputs from a file sendhil Shell Programming and Scripting 4 02-01-2006 05:48 AM
Multiple Inputs douknownam Shell Programming and Scripting 4 05-25-2004 01:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-24-2007
kpatel786 kpatel786 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 12
Please give your inputs !!!!

I am trying to extract two fields from the output of ifconfig command on one of my sun server . The output looks like :
root@e08k18:/tmp/test# ifconfig -a
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
inet 127.0.0.1 netmask ff000000
ce0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
inet 10.177.4.61 netmask ffffff00 broadcast 10.177.4.255
groupname ipmp0
ether 0:3:ba:6c:7f:2e

I require output as field1 filed2 for all the interfaces ...but when I try
la0 127.0.01
ce0 10.177.4.61

# ifconfig |awk -F ":" {print $1 $3)'

However I am not getting the required output.
Any clue in this regard will be of great help.
Thanking you in advance.
  #2 (permalink)  
Old 04-24-2007
Deal_NoDeal Deal_NoDeal is offline
Registered User
  
 

Join Date: Feb 2007
Location: Boston, MA
Posts: 64
Quote:
Originally Posted by kpatel786
I am trying to extract two fields from the output of ifconfig command on one of my sun server . The output looks like :
root@e08k18:/tmp/test# ifconfig -a
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
inet 127.0.0.1 netmask ff000000
ce0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
inet 10.177.4.61 netmask ffffff00 broadcast 10.177.4.255
groupname ipmp0
ether 0:3:ba:6c:7f:2e

I require output as field1 filed2 for all the interfaces ...but when I try
la0 127.0.01
ce0 10.177.4.61

# ifconfig |awk -F ":" {print $1 $3)'

However I am not getting the required output.
Any clue in this regard will be of great help.
Thanking you in advance.
The output of ifconfig are in different lines. You will have to join the lines and then maybe use awk. An example would be like this:

-------------
#!/bin/sh

ifconfig -a | awk -F":" '{print $1}' |
awk 'BEGIN {x = 0}
{
if (x<2) {
printf("%s ",$0)
x=x+1
}
if (x==2) {
printf("\n")
x = 0
}
}' | awk '{print $1, $3}'
-------------
  #3 (permalink)  
Old 04-24-2007
kpatel786 kpatel786 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 12
Thanks for your reply I have tried this :-

ifconfig -a | awk -F":" '{print $1}' |
awk 'BEGIN {x = 0}
{
if (x<2) {
printf("%s ",$0)
x=x+1
}
if (x==2) {
printf("\n")
x = 0
}
}' | awk '{print $1, $3}'

and it gives the output as :-
ce2 172.16.0.129
ce6 172.16.1.1
clprivnet0 172.16.193.1
ce0 10.177.4.61
ce0:1 10.177.4.70
ce0:2 10.177.4.66
ce0:3 10.177.4.67
ce0:4 10.177.4.65
ce4 10.177.4.62
ce3 10.177.224.80
ce0 10.177.4.61

Thanks once again !!!!!
  #4 (permalink)  
Old 04-25-2007
Deal_NoDeal Deal_NoDeal is offline
Registered User
  
 

Join Date: Feb 2007
Location: Boston, MA
Posts: 64
Quote:
Originally Posted by kpatel786
Thanks for your reply I have tried this :-

ifconfig -a | awk -F":" '{print $1}' |
awk 'BEGIN {x = 0}
{
if (x<2) {
printf("%s ",$0)
x=x+1
}
if (x==2) {
printf("\n")
x = 0
}
}' | awk '{print $1, $3}'

and it gives the output as :-
ce2 172.16.0.129
ce6 172.16.1.1
clprivnet0 172.16.193.1
ce0 10.177.4.61
ce0:1 10.177.4.70
ce0:2 10.177.4.66
ce0:3 10.177.4.67
ce0:4 10.177.4.65
ce4 10.177.4.62
ce3 10.177.224.80
ce0 10.177.4.61

Thanks once again !!!!!
A shorter solution is also this:

ifconfig -a | awk -F":" '{print $1}' | paste - - | awk '{print $1, $3}'
  #5 (permalink)  
Old 04-25-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
Quote:
Originally Posted by Deal_NoDeal
A shorter solution is also this:

ifconfig -a | awk -F":" '{print $1}' | paste - - | awk '{print $1, $3}'

Shorter (but not by much), slower (2 unnecessary external commands), and incorrect for the sample given:


Code:
lo0 127.0.0.1
ce0 10.177.4.61
groupname ether


  #6 (permalink)  
Old 04-26-2007
Deal_NoDeal Deal_NoDeal is offline
Registered User
  
 

Join Date: Feb 2007
Location: Boston, MA
Posts: 64
Quote:
Originally Posted by cfajohnson

Shorter (but not by much), slower (2 unnecessary external commands), and incorrect for the sample given:


Code:
lo0 127.0.0.1
ce0 10.177.4.61
groupname ether


True its not the fastest solution but ifconfig wouldn't have 1000s of lines of output so I would say it won't hurt but thanks for the point.
  #7 (permalink)  
Old 04-25-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
Please use a title that describes your problem.

Quote:
Originally Posted by kpatel786
I am trying to extract two fields from the output of ifconfig command on one of my sun server .
...
I require output as field1 filed2 for all the interfaces ...but when I try
la0 127.0.01
ce0 10.177.4.61

# ifconfig |awk -F ":" {print $1 $3)'

However I am not getting the required output.


Code:
ifconfig |
 awk '
  $1 ~ /:$/ { sub( /:$/, "", $1)
                    ifname = $1 }
  $1 == "inet" { print ifname, $2 }
'

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0