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 > awk field separation
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

awk field separation

Réponse
 
LinkBack Outils de la discussion
Vieux 21/07/2007, 05h27   #1
smarts_buy@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut awk field separation

Hi,

I got a string below

Field1=1111 Field2=222 Field3=3333333
Field1='1111 11111' Field2=222 Field3=3333333 Field5=555555

How can I do the below

1) extract the fields, my problem is having a space in between the
quotes.
2) To extract all the unique fileds, Field1, Feild2..

Currently is wat I use to extract the fields.

for(i=1;i<=NF;==1){
if ($i~/^Field1=/)
field1=$i
}

  Réponse avec citation
Vieux 21/07/2007, 06h00   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

smarts_buy@yahoo.com wrote:
> Hi,
>
> I got a string below
>
> Field1=1111 Field2=222 Field3=3333333
> Field1='1111 11111' Field2=222 Field3=3333333 Field5=555555
>
> How can I do the below
>
> 1) extract the fields, my problem is having a space in between the
> quotes.
> 2) To extract all the unique fileds, Field1, Feild2..
>
> Currently is wat I use to extract the fields.
>
> for(i=1;i<=NF;==1){
> if ($i~/^Field1=/)
> field1=$i
> }
>


awk -F= '{
name=$1
for (i=2;i<=NF;i++) {
nn=val=$i
sub(/.*[[:space:]]/,"",nn)
sub(/[[:space:]]+[^[:space:]]+$/,"",val)
printf "%s=%s\n",name,val
name=nn
}
}' file

Regards,

Ed.
  Réponse avec citation
Vieux 21/07/2007, 06h24   #3
smarts_buy@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

On Jul 21, 1:00 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> smarts_...@yahoo.com wrote:
> > Hi,

>
> > I got a string below

>
> > Field1=1111 Field2=222 Field3=3333333
> > Field1='1111 11111' Field2=222 Field3=3333333 Field5=555555

>
> > How can I do the below

>
> > 1) extract the fields, my problem is having a space in between the
> > quotes.
> > 2) To extract all the unique fileds, Field1, Feild2..

>
> > Currently is wat I use to extract the fields.

>
> > for(i=1;i<=NF;==1){
> > if ($i~/^Field1=/)
> > field1=$i
> > }

>
> awk -F= '{
> name=$1
> for (i=2;i<=NF;i++) {
> nn=val=$i
> sub(/.*[[:space:]]/,"",nn)
> sub(/[[:space:]]+[^[:space:]]+$/,"",val)
> printf "%s=%s\n",name,val
> name=nn
> }
>
> }' file
>
> Regards,
>
> Ed.


Thanks for the reply.

Forgot to highlight one point. What I need exactly is an output of the
format. The name of the field is not standard, which prevents me from
any kind of sorting.

field1='111 111', field2=2222, field3=3333


My data is not ordered like below.

Field1=1111 Field3=3333333 Field2=222
Field1='1111 11111' Field5=555555 Field2=222 Field3=3333333

Thanks

  Réponse avec citation
Vieux 21/07/2007, 06h29   #4
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

smarts_buy@yahoo.com wrote:
> On Jul 21, 1:00 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>>smarts_...@yahoo.com wrote:
>>
>>>Hi,

>>
>>>I got a string below

>>
>>>Field1=1111 Field2=222 Field3=3333333
>>>Field1='1111 11111' Field2=222 Field3=3333333 Field5=555555

>>
>>>How can I do the below

>>
>>>1) extract the fields, my problem is having a space in between the
>>>quotes.
>>>2) To extract all the unique fileds, Field1, Feild2..

>>
>>>Currently is wat I use to extract the fields.

>>
>>>for(i=1;i<=NF;==1){
>>>if ($i~/^Field1=/)
>>> field1=$i
>>>}

>>
>>awk -F= '{
>> name=$1
>> for (i=2;i<=NF;i++) {
>> nn=val=$i
>> sub(/.*[[:space:]]/,"",nn)
>> sub(/[[:space:]]+[^[:space:]]+$/,"",val)
>> printf "%s=%s\n",name,val
>> name=nn
>> }
>>
>>}' file
>>
>>Regards,
>>
>> Ed.

>
>
> Thanks for the reply.
>
> Forgot to highlight one point. What I need exactly is an output of the
> format. The name of the field is not standard, which prevents me from
> any kind of sorting.
>
> field1='111 111', field2=2222, field3=3333
>
>
> My data is not ordered like below.
>
> Field1=1111 Field3=3333333 Field2=222
> Field1='1111 11111' Field5=555555 Field2=222 Field3=3333333
>
> Thanks
>


Whatever point your trying to make, I don't understand. Are you saying
there's some problem with the solution I provided and, if so, what is
the problem? It looks like you're now saying you have commas between
your fields. If so, the solution would be totally different as your
problem is much simpler. Please state what you're trying to do along
with sample input and desired output.

Ed.

Ed.
  Réponse avec citation
Vieux 21/07/2007, 06h37   #5
smarts_buy@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

On Jul 21, 1:29 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> smarts_...@yahoo.com wrote:
> > On Jul 21, 1:00 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> >>smarts_...@yahoo.com wrote:

>
> >>>Hi,

>
> >>>I got a string below

>
> >>>Field1=1111 Field2=222 Field3=3333333
> >>>Field1='1111 11111' Field2=222 Field3=3333333 Field5=555555

>
> >>>How can I do the below

>
> >>>1) extract the fields, my problem is having a space in between the
> >>>quotes.
> >>>2) To extract all the unique fileds, Field1, Feild2..

>
> >>>Currently is wat I use to extract the fields.

>
> >>>for(i=1;i<=NF;==1){
> >>>if ($i~/^Field1=/)
> >>> field1=$i
> >>>}

>
> >>awk -F= '{
> >> name=$1
> >> for (i=2;i<=NF;i++) {
> >> nn=val=$i
> >> sub(/.*[[:space:]]/,"",nn)
> >> sub(/[[:space:]]+[^[:space:]]+$/,"",val)
> >> printf "%s=%s\n",name,val
> >> name=nn
> >> }

>
> >>}' file

>
> >>Regards,

>
> >> Ed.

>
> > Thanks for the reply.

>
> > Forgot to highlight one point. What I need exactly is an output of the
> > format. The name of the field is not standard, which prevents me from
> > any kind of sorting.

>
> > field1='111 111', field2=2222, field3=3333

>
> > My data is not ordered like below.

>
> > Field1=1111 Field3=3333333 Field2=222
> > Field1='1111 11111' Field5=555555 Field2=222 Field3=3333333

>
> > Thanks

>
> Whatever point your trying to make, I don't understand. Are you saying
> there's some problem with the solution I provided and, if so, what is
> the problem? It looks like you're now saying you have commas between
> your fields. If so, the solution would be totally different as your
> problem is much simpler. Please state what you're trying to do along
> with sample input and desired output.
>
> Ed.
>
> Ed.


Sorry for the confusion,

My Input: It's space separated, but there is space insome fields
delimited by quotes.

name='Some name' srcip=ipaddr:1.1.1. port=1234 destip=ipaddr:2.2.2.2
comments='some comments'
name=NospaceName srcip=ipaddr:1.1.1. destip=ipaddr:2.2.2.2

My Output:
name='Some name' srcip=ipaddr:1.1.1, destip=ipaddr:2.2.2.2, port=1234
name=NospaceName srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2, port=Any

Thanks


  Réponse avec citation
Vieux 21/07/2007, 12h49   #6
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

smarts_buy@yahoo.com wrote:
> On Jul 21, 1:29 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>>smarts_...@yahoo.com wrote:
>>
>>>On Jul 21, 1:00 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>>
>>>>smarts_...@yahoo.com wrote:

>>
>>>>>Hi,

>>
>>>>>I got a string below

>>
>>>>>Field1=1111 Field2=222 Field3=3333333
>>>>>Field1='1111 11111' Field2=222 Field3=3333333 Field5=555555

>>
>>>>>How can I do the below

>>
>>>>>1) extract the fields, my problem is having a space in between the
>>>>>quotes.
>>>>>2) To extract all the unique fileds, Field1, Feild2..

>>
>>>>>Currently is wat I use to extract the fields.

>>
>>>>>for(i=1;i<=NF;==1){
>>>>>if ($i~/^Field1=/)
>>>>> field1=$i
>>>>>}

>>
>>>>awk -F= '{
>>>> name=$1
>>>> for (i=2;i<=NF;i++) {
>>>> nn=val=$i
>>>> sub(/.*[[:space:]]/,"",nn)
>>>> sub(/[[:space:]]+[^[:space:]]+$/,"",val)
>>>> printf "%s=%s\n",name,val
>>>> name=nn
>>>> }

>>
>>>>}' file

<snip>
> Sorry for the confusion,
>
> My Input: It's space separated, but there is space insome fields
> delimited by quotes.


Yes, I know. The script I posted deals with that.

> name='Some name' srcip=ipaddr:1.1.1. port=1234 destip=ipaddr:2.2.2.2
> comments='some comments'
> name=NospaceName srcip=ipaddr:1.1.1. destip=ipaddr:2.2.2.2
>
> My Output:
> name='Some name' srcip=ipaddr:1.1.1, destip=ipaddr:2.2.2.2, port=1234
> name=NospaceName srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2, port=Any


That's pretty random-looking output format. Sometimes there's a comma,
sometimes there's a comma and some white space, sometimes there's white
space but no comma, one field gets stipped, another field gets added, etc.

I can't guess what the rules are you're applying to create that output,
so at the point of the "printf" in the script I posted, you've got the
name and value of each field, so just do whatever it is you want with
the output formatting at that point, testing the field name against the
"name" variable if necessary.

Ed.
  Réponse avec citation
Vieux 21/07/2007, 18h45   #7
smarts_buy@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

On Jul 21, 7:49 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> smarts_...@yahoo.com wrote:
> > On Jul 21, 1:29 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> >>smarts_...@yahoo.com wrote:

>
> >>>On Jul 21, 1:00 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> >>>>smarts_...@yahoo.com wrote:

>
> >>>>>Hi,

>
> >>>>>I got a string below

>
> >>>>>Field1=1111 Field2=222 Field3=3333333
> >>>>>Field1='1111 11111' Field2=222 Field3=3333333 Field5=555555

>
> >>>>>How can I do the below

>
> >>>>>1) extract the fields, my problem is having a space in between the
> >>>>>quotes.
> >>>>>2) To extract all the unique fileds, Field1, Feild2..

>
> >>>>>Currently is wat I use to extract the fields.

>
> >>>>>for(i=1;i<=NF;==1){
> >>>>>if ($i~/^Field1=/)
> >>>>> field1=$i
> >>>>>}

>
> >>>>awk -F= '{
> >>>> name=$1
> >>>> for (i=2;i<=NF;i++) {
> >>>> nn=val=$i
> >>>> sub(/.*[[:space:]]/,"",nn)
> >>>> sub(/[[:space:]]+[^[:space:]]+$/,"",val)
> >>>> printf "%s=%s\n",name,val
> >>>> name=nn
> >>>> }

>
> >>>>}' file

> <snip>
> > Sorry for the confusion,

>
> > My Input: It's space separated, but there is space insome fields
> > delimited by quotes.

>
> Yes, I know. The script I posted deals with that.
>
> > name='Some name' srcip=ipaddr:1.1.1. port=1234 destip=ipaddr:2.2.2.2
> > comments='some comments'
> > name=NospaceName srcip=ipaddr:1.1.1. destip=ipaddr:2.2.2.2

>
> > My Output:
> > name='Some name' srcip=ipaddr:1.1.1, destip=ipaddr:2.2.2.2, port=1234
> > name=NospaceName srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2, port=Any

>
> That's pretty random-looking output format. Sometimes there's a comma,
> sometimes there's a comma and some white space, sometimes there's white
> space but no comma, one field gets stipped, another field gets added, etc.
>
> I can't guess what the rules are you're applying to create that output,
> so at the point of the "printf" in the script I posted, you've got the
> name and value of each field, so just do whatever it is you want with
> the output formatting at that point, testing the field name against the
> "name" variable if necessary.
>
> Ed.

Hi Ed,

Let me try one more time to explain it. My problem is the field with
space which is delimited by quotes. In fact the output fields are all
separated by commas. I missed the comma in the output. Field names are
standard. But not put in any order. I'm interested in only certain
fields and if that field is missing, it should be replaced with 'Any'
as shown below.

Sorry I was not again clear in my explanation and that annoyed you.
The solution will big time for me. thanks again

My input:
name='Some name' srcip=ipaddr:1.1.1 port=1234 destip=ipaddr:2.2.2.2
comments='No relavant'
name=NospaceName srcip=ipaddr:1.1.1 destip=ipaddr:2.2.2.2

My Output:

name='Some name',srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2,por t=1234
name=NospaceName,srcip=ipaddr:1.1.1,destip=ipaddr: 2.2.2.2,port=Any

  Réponse avec citation
Vieux 22/07/2007, 04h25   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

smarts_buy@yahoo.com wrote:
<snip>
> Let me try one more time to explain it. My problem is the field with
> space which is delimited by quotes. In fact the output fields are all
> separated by commas. I missed the comma in the output. Field names are
> standard. But not put in any order. I'm interested in only certain
> fields and if that field is missing, it should be replaced with 'Any'
> as shown below.
>
> Sorry I was not again clear in my explanation and that annoyed you.
> The solution will big time for me. thanks again
>
> My input:
> name='Some name' srcip=ipaddr:1.1.1 port=1234 destip=ipaddr:2.2.2.2
> comments='No relavant'
> name=NospaceName srcip=ipaddr:1.1.1 destip=ipaddr:2.2.2.2
>
> My Output:
>
> name='Some name',srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2,por t=1234
> name=NospaceName,srcip=ipaddr:1.1.1,destip=ipaddr: 2.2.2.2,port=Any
>


Is this what you want:

$ cat file
name='Some name' srcip=ipaddr:1.1.1 port=1234 destip=ipaddr:2.2.2.2
comments='No relavant'
name=NospaceName srcip=ipaddr:1.1.1 destip=ipaddr:2.2.2.2
$ awk -F= 'BEGIN { nf=split("name srcip destip port",field," ") }
{
name=$1
for (i=2;i<NF;i++) {
nn=v=$i
sub(/.*[[:space:]]/,"",nn)
sub(/[[:space:]]+[^[:space:]]+$/,"",v)
val[name]=v
name=nn
}
val[name]=$i
sep=""
for (i=1; i<=nf; i++) {
name=field[i]
printf "%s%s=%s",sep,name,name in val?val[name]:"Any"
delete val[name]
sep=","
}
print ""
}' file
name='Some name',srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2,por t=1234
name=NospaceName,srcip=ipaddr:1.1.1,destip=ipaddr: 2.2.2.2,port=Any

Regards,

Ed.
  Réponse avec citation
Vieux 22/07/2007, 08h36   #9
smarts_buy@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

On Jul 22, 11:25 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> smarts_...@yahoo.com wrote:
>
> <snip>
>
>
>
> > Let me try one more time to explain it. My problem is the field with
> > space which is delimited by quotes. In fact the output fields are all
> > separated by commas. I missed the comma in the output. Field names are
> > standard. But not put in any order. I'm interested in only certain
> > fields and if that field is missing, it should be replaced with 'Any'
> > as shown below.

>
> > Sorry I was not again clear in my explanation and that annoyed you.
> > The solution will big time for me. thanks again

>
> > My input:
> > name='Some name' srcip=ipaddr:1.1.1 port=1234 destip=ipaddr:2.2.2.2
> > comments='No relavant'
> > name=NospaceName srcip=ipaddr:1.1.1 destip=ipaddr:2.2.2.2

>
> > My Output:

>
> > name='Some name',srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2,por t=1234
> > name=NospaceName,srcip=ipaddr:1.1.1,destip=ipaddr: 2.2.2.2,port=Any

>
> Is this what you want:
>
> $ cat file
> name='Some name' srcip=ipaddr:1.1.1 port=1234 destip=ipaddr:2.2.2.2
> comments='No relavant'
> name=NospaceName srcip=ipaddr:1.1.1 destip=ipaddr:2.2.2.2
> $ awk -F= 'BEGIN { nf=split("name srcip destip port",field," ") }
> {
> name=$1
> for (i=2;i<NF;i++) {
> nn=v=$i
> sub(/.*[[:space:]]/,"",nn)
> sub(/[[:space:]]+[^[:space:]]+$/,"",v)
> val[name]=v
> name=nn
> }
> val[name]=$i
> sep=""
> for (i=1; i<=nf; i++) {
> name=field[i]
> printf "%s%s=%s",sep,name,name in val?val[name]:"Any"
> delete val[name]
> sep=","
> }
> print ""}' file
>
> name='Some name',srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2,por t=1234
> name=NospaceName,srcip=ipaddr:1.1.1,destip=ipaddr: 2.2.2.2,port=Any
>
> Regards,
>
> Ed.


Thanks Ed. It worked almost perfectly as I wanted. Now I'm trying to
figure out how all worked together. Just curious, how long you spent
on this?

Thanks again.

  Réponse avec citation
Vieux 22/07/2007, 12h48   #10
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk field separation

smarts_buy@yahoo.com wrote:
> On Jul 22, 11:25 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>>smarts_...@yahoo.com wrote:
>>
>><snip>
>>
>>
>>
>>>Let me try one more time to explain it. My problem is the field with
>>>space which is delimited by quotes. In fact the output fields are all
>>>separated by commas. I missed the comma in the output. Field names are
>>>standard. But not put in any order. I'm interested in only certain
>>>fields and if that field is missing, it should be replaced with 'Any'
>>>as shown below.

>>
>>>Sorry I was not again clear in my explanation and that annoyed you.
>>>The solution will big time for me. thanks again

>>
>>>My input:
>>>name='Some name' srcip=ipaddr:1.1.1 port=1234 destip=ipaddr:2.2.2.2
>>>comments='No relavant'
>>>name=NospaceName srcip=ipaddr:1.1.1 destip=ipaddr:2.2.2.2

>>
>>>My Output:

>>
>>>name='Some name',srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2,por t=1234
>>>name=NospaceName,srcip=ipaddr:1.1.1,destip=ipad dr:2.2.2.2,port=Any

>>
>>Is this what you want:
>>
>>$ cat file
>>name='Some name' srcip=ipaddr:1.1.1 port=1234 destip=ipaddr:2.2.2.2
>>comments='No relavant'
>>name=NospaceName srcip=ipaddr:1.1.1 destip=ipaddr:2.2.2.2
>>$ awk -F= 'BEGIN { nf=split("name srcip destip port",field," ") }
>>{
>> name=$1
>> for (i=2;i<NF;i++) {
>> nn=v=$i
>> sub(/.*[[:space:]]/,"",nn)
>> sub(/[[:space:]]+[^[:space:]]+$/,"",v)
>> val[name]=v
>> name=nn
>> }
>> val[name]=$i
>> sep=""
>> for (i=1; i<=nf; i++) {
>> name=field[i]
>> printf "%s%s=%s",sep,name,name in val?val[name]:"Any"
>> delete val[name]
>> sep=","
>> }
>> print ""}' file
>>
>>name='Some name',srcip=ipaddr:1.1.1,destip=ipaddr:2.2.2.2,por t=1234
>>name=NospaceName,srcip=ipaddr:1.1.1,destip=ipadd r:2.2.2.2,port=Any
>>
>>Regards,
>>
>> Ed.

>
>
> Thanks Ed. It worked almost perfectly as I wanted. Now I'm trying to
> figure out how all worked together. Just curious, how long you spent
> on this?
>
> Thanks again.
>


About 10 minutes for the first script, then about 10 minutes again for
the final one.

Ed.
  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 09h55.


É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,26317 seconds with 18 queries