|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|