Please give your inputs !!!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please give your inputs !!!!
# 1  
Old 04-24-2007
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  
Old 04-24-2007
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  
Old 04-24-2007
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  
Old 04-25-2007
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 }
'

# 5  
Old 04-25-2007
ifconfig | awk ' /flags/ { x = $1 ; next } /inet/ { print x $2 }' Smilie
# 6  
Old 04-25-2007
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}'
# 7  
Old 04-25-2007
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


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inputs argument for sh -c

I have doubts with the following command: % find "$1" -name "*.html" -print0 | sort -zn | xargs -r -0 -n 1 sh -c 'echo "Dumping file: $2" >> "$1"; w3m "$2" >> "$1" 2>&1 ' sh "$2" I have doubts in the input arguments value i.e. $0, $1, $2... Step by step: 1.- % find "$1" -name "*.html"... (10 Replies)
Discussion started by: puertas12
10 Replies

2. Shell Programming and Scripting

Taking inputs on prompt

need a help to get a script: bash # ./xx.sh >> count 567 script will run the xx.sh and it will go to >> then run "count" will get the result "567" and print it (7 Replies)
Discussion started by: Aditya.Gurgaon
7 Replies

3. Shell Programming and Scripting

Read variable inputs

I am trying to make an interactive script. Only problem my inputs are not read and correctly channeled. Please help: Here is my code #!/bin/sh PATHSCRIPT=/home/pp/tmp #if ; then # echo "Syntax : $0 input off lat sample" # exit 1 # fi echo "Choice of Graph" echo "1 -- Type... (5 Replies)
Discussion started by: newkid.7955
5 Replies

4. Shell Programming and Scripting

Give inputs externally to awk script

Hello to all, Please some awk expert could help me. If I want to run an awk script as "command" give it inputs externally I do: Script.sh Input="$1" # "$1" is the input that will be given to the script Output=${Input%.*}.csv awk '{$1=$1}1' $Input | awk '{...}' > $Output and I run the... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

5. UNIX for Dummies Questions & Answers

How to give multiple inputs to a shell script

Got struck while trying to write a shell script which should automatically give input. While running a script for eg: (adpatch.sh) It Prompts for Multiple inputs like: Do you currently have files used for installing or upgrading the database installed in this APPL_TOP ? need to give... (2 Replies)
Discussion started by: abdmoha
2 Replies

6. Programming

Handling variable inputs

Hi, Is there any way to handle variable inputs. If an application handles more than one input how to print the input values. Example : ./a.out 1 "hi" 3 ./a.out 1 ./a.out 1 3 4 "hello" output should be: 1 hi 3 1 (1 Reply)
Discussion started by: yhacks
1 Replies

7. Shell Programming and Scripting

Check for numeric inputs

Hi All, How do i modify the below script such that if the input is numeric, it will give the numeric digit, else it will ouput "0" echo "xxx" | awk '/^+$/' (6 Replies)
Discussion started by: Raynon
6 Replies

8. Shell Programming and Scripting

Validating inputs from a file

Hi, I have a file called inputs. Now that file has the values like this: 1 2 3 Now In my script called 'get.sh' I do this : exec < inputs read a b c d Now I know that there will not be any value in d. How can I check it. I need the exact condition for checking whether the variable has... (1 Reply)
Discussion started by: sendhilmani123
1 Replies

9. Shell Programming and Scripting

Inputs from a file

Hi, I have a shell script that has to taken inputs from a file say "Inputs". Now I take 2 inputs at a time. Suppose the Inputs file contains numbers like 2 3 4 5 Now I have a written a script for adding 2 numbers. When I run the script for first time 2 and 3 must be the inputs. When i run the... (4 Replies)
Discussion started by: sendhil
4 Replies

10. Shell Programming and Scripting

Multiple Inputs

Have tried the search, but nothing resembles what I'd like to accomplish. I am attempting to write a script that will allow the user to input a list of data at the command prompt, then the data is used by another script for processing. I am allowing the user a list of 10 members in order to... (4 Replies)
Discussion started by: douknownam
4 Replies
Login or Register to Ask a Question