PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > one liner sed or awk
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

one liner sed or awk

Réponse
 
LinkBack Outils de la discussion
Vieux 30/05/2007, 06h41   #1
Seve.Paritos@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut one liner sed or awk

hello everyone.
I have the following ifconfig output on solaris and want to be able to
get the output from it in the following format using a one liner.

ce0 192.168.13.50
ce0:1 192.168.13.245
ce0:2 192.168.13.64
ce0:3 192.168.13.62
ce1 192.168.13.51

bash-2.05$ ifconfig -a
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index
3
inet 127.0.0.1 netmask ff000000
ce0:
flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>
mtu 1500 index 4
inet 192.168.13.50 netmask fffffc00 broadcast 192.168.15.255
groupname amazon_ipmp_bk
ce0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500
index 4
inet 192.168.13.245 netmask fffffc00 broadcast 192.168.15.255
ce0:2: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
4
inet 192.168.13.64 netmask fffffc00 broadcast 192.168.15.255
ce0:3: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
4
inet 192.168.13.62 netmask fffffc00 broadcast 192.168.15.255
ce1:
flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>
mtu 1500 index 5
inet 192.168.13.51 netmask fffffc00 broadcast 192.168.15.255
groupname amazon_ipmp_bk

Thanks

  Réponse avec citation
Vieux 30/05/2007, 12h19   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: one liner sed or awk

Seve.Paritos@gmail.com wrote:
> hello everyone.
> I have the following ifconfig output on solaris and want to be able to
> get the output from it in the following format using a one liner.
>
> ce0 192.168.13.50
> ce0:1 192.168.13.245
> ce0:2 192.168.13.64
> ce0:3 192.168.13.62
> ce1 192.168.13.51
>
> bash-2.05$ ifconfig -a
> lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index
> 3
> inet 127.0.0.1 netmask ff000000
> ce0:
> flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>
> mtu 1500 index 4
> inet 192.168.13.50 netmask fffffc00 broadcast 192.168.15.255
> groupname amazon_ipmp_bk
> ce0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500
> index 4
> inet 192.168.13.245 netmask fffffc00 broadcast 192.168.15.255
> ce0:2: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
> 4
> inet 192.168.13.64 netmask fffffc00 broadcast 192.168.15.255
> ce0:3: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
> 4
> inet 192.168.13.62 netmask fffffc00 broadcast 192.168.15.255
> ce1:
> flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>
> mtu 1500 index 5
> inet 192.168.13.51 netmask fffffc00 broadcast 192.168.15.255
> groupname amazon_ipmp_bk
>
> Thanks
>


It's hard to tell how much of the formatting above is due to
linewrapping in your posting, but try this:

awk 'key ~ /^c/{print key,$2} {key = sub(/:$/,"",$1) ? $1 : ""}'

Ed.
  Réponse avec citation
Vieux 30/05/2007, 17h43   #3
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: one liner sed or awk

Seve.Paritos@gmail.com wrote:
> hello everyone.
> I have the following ifconfig output on solaris and want to be able to
> get the output from it in the following format using a one liner.
>
> ce0 192.168.13.50
> ce0:1 192.168.13.245
> ce0:2 192.168.13.64
> ce0:3 192.168.13.62
> ce1 192.168.13.51
>
> bash-2.05$ ifconfig -a
> lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index
> 3
> inet 127.0.0.1 netmask ff000000
> ce0:
> flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>
> mtu 1500 index 4
> inet 192.168.13.50 netmask fffffc00 broadcast 192.168.15.255
> groupname amazon_ipmp_bk
> ce0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500
> index 4
> inet 192.168.13.245 netmask fffffc00 broadcast 192.168.15.255
> ce0:2: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
> 4
> inet 192.168.13.64 netmask fffffc00 broadcast 192.168.15.255
> ce0:3: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
> 4
> inet 192.168.13.62 netmask fffffc00 broadcast 192.168.15.255
> ce1:
> flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>
> mtu 1500 index 5
> inet 192.168.13.51 netmask fffffc00 broadcast 192.168.15.255
> groupname amazon_ipmp_bk
>
> Thanks
>


ifconfig -a |
sed -n 'h;s/^\([^ ]*\):.*/\1/p;g;s/.*inet \([^ ]*\).*/\1/p'

awk has better formatting capabilities:

ifconfig -a |
awk '$1~/:$/ {s=$1} {for(i=1;i<NF;++i) if($i=="inet") print s,$(i+1)}'

printf "%s\t%s\n",substr(s,1,length(s)-1),$(i+1)

formats the output as TAB-separated, and the substr() eliminates the
trailing :


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 30/05/2007, 17h48   #4
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: one liner sed or awk

Ed Morton wrote:
> Seve.Paritos@gmail.com wrote:
>> hello everyone.
>> I have the following ifconfig output on solaris and want to be able to
>> get the output from it in the following format using a one liner.
>>
>> ce0 192.168.13.50
>> ce0:1 192.168.13.245
>> ce0:2 192.168.13.64
>> ce0:3 192.168.13.62
>> ce1 192.168.13.51
>>
>> bash-2.05$ ifconfig -a
>> lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index
>> 3
>> inet 127.0.0.1 netmask ff000000
>> ce0:
>> flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>
>> mtu 1500 index 4
>> inet 192.168.13.50 netmask fffffc00 broadcast 192.168.15.255
>> groupname amazon_ipmp_bk
>> ce0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500
>> index 4
>> inet 192.168.13.245 netmask fffffc00 broadcast 192.168.15.255
>> ce0:2: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
>> 4
>> inet 192.168.13.64 netmask fffffc00 broadcast 192.168.15.255
>> ce0:3: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
>> 4
>> inet 192.168.13.62 netmask fffffc00 broadcast 192.168.15.255
>> ce1:
>> flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>
>> mtu 1500 index 5
>> inet 192.168.13.51 netmask fffffc00 broadcast 192.168.15.255
>> groupname amazon_ipmp_bk
>>
>> Thanks
>>

>
> It's hard to tell how much of the formatting above is due to
> linewrapping in your posting, but try this:
>
> awk 'key ~ /^c/{print key,$2} {key = sub(/:$/,"",$1) ? $1 : ""}'
>
> Ed.


Each interface has got 2 or more lines actually.
Consecutive lines are indented.

The question would fit better for comp.unix.admin or comp.unix.solaris


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 30/05/2007, 19h34   #5
Robert Bonomi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: one liner sed or awk

In article <1180503661.602541.126320@q69g2000hsb.googlegroups .com>,
<Seve.Paritos@gmail.com> wrote:
>hello everyone.
>I have the following ifconfig output on solaris and want to be able to
>get the output from it in the following format using a one liner.
>
>ce0 192.168.13.50
>ce0:1 192.168.13.245
>ce0:2 192.168.13.64
>ce0:3 192.168.13.62
>ce1 192.168.13.51
>
>bash-2.05$ ifconfig -a
>lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index
>3
> inet 127.0.0.1 netmask ff000000
>ce0:
>flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPR ECATED,IPv4,NOFAILOVER>
>mtu 1500 index 4
> inet 192.168.13.50 netmask fffffc00 broadcast 192.168.15.255
> groupname amazon_ipmp_bk
>ce0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500
>index 4
> inet 192.168.13.245 netmask fffffc00 broadcast 192.168.15.255
>ce0:2: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
>4
> inet 192.168.13.64 netmask fffffc00 broadcast 192.168.15.255
>ce0:3: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index
>4
> inet 192.168.13.62 netmask fffffc00 broadcast 192.168.15.255
>ce1: flags=9040843<UP,BROADCAST,RUNNING,MULTICAST,DEPRE CATED,IPv4,NOFAILOVER>

mtu 1500 index 5
> inet 192.168.13.51 netmask fffffc00 broadcast 192.168.15.255
> groupname amazon_ipmp_bk
>
>Thanks
>


from the dirty tricks department:

ifconfig -a \
awk '/flags/ {sub(":$","",$1);f=$1;} /inet/ {printf ("%s\t%s\n",f,$2)}'


Note: This will generate a separate, full, line for each address assigned to
an interface with multiple addresses.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h50.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19388 seconds with 13 queries