parsing name value pairs

  • Follow


All,

I have the the follwing input:

docid=00000850 volume=0006 subvolume=0026
docid=00000851 volume=0046 subvolume=0006
docid=00000852 volume=0036 subvolume=0086

What I want am looking to do is to parse the name values pairs
into an array with out being depandant on position of the input.

For example I dont want is code that says something like this:
docid=$(cut -c7-15)

What I am looking for is to be able to reference the values of an array
with a literal name something like this:

array["docid"]
array["volume"]
array["subvolume"]

Can somebody provide me with an example.

Thanks in advance for all that answer this post.

0
Reply beefstu350 (68) 2/15/2005 7:12:23 PM


Stu wrote:
> All,
> 
> I have the the follwing input:
> 
> docid=00000850 volume=0006 subvolume=0026
> docid=00000851 volume=0046 subvolume=0006
> docid=00000852 volume=0036 subvolume=0086
> 
> What I want am looking to do is to parse the name values pairs
> into an array with out being depandant on position of the input.
> 
> For example I dont want is code that says something like this:
> docid=$(cut -c7-15)
> 
> What I am looking for is to be able to reference the values of an array
> with a literal name something like this:
> 
> array["docid"]
> array["volume"]
> array["subvolume"]
> 
> Can somebody provide me with an example.
> 
> Thanks in advance for all that answer this post.

Try this:

gawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2}array[$i]=$(i+1)}
	{print array["docid"],array["volume"],array["subvolume"] }' file

Regards,

	Ed.
0
Reply Ed 2/15/2005 7:46:01 PM


In article <cutjib$ii6@netnews.proxy.lucent.com>,
Ed Morton  <morton@lsupcaemnt.com> wrote:
>
>
>Stu wrote:
>> All,
>> 
>> I have the the follwing input:
>> 
>> docid=00000850 volume=0006 subvolume=0026
>> docid=00000851 volume=0046 subvolume=0006
>> docid=00000852 volume=0036 subvolume=0086
>> 
>> What I want am looking to do is to parse the name values pairs
>> into an array with out being depandant on position of the input.
>> 
>> For example I dont want is code that says something like this:
>> docid=$(cut -c7-15)
>> 
>> What I am looking for is to be able to reference the values of an array
>> with a literal name something like this:
>> 
>> array["docid"]
>> array["volume"]
>> array["subvolume"]
>> 
>> Can somebody provide me with an example.
>> 
>> Thanks in advance for all that answer this post.
>
>Try this:
>
>gawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2}array[$i]=$(i+1)}
>	{print array["docid"],array["volume"],array["subvolume"] }' file

Isn't that a wee tad more difficult than it ought to be?

{
for (i=1; i<=NF; i++)
    A[T[1]] = T[split($i,T,"=")]
}
END	{ now_everything_is_in_A() }

And, lest anyone ask, I don't like solutions that involve changing the
built-in variables (unless it's really necessary).

0
Reply gazelle 2/15/2005 7:55:42 PM

Ed,

The following did not print any output.

nawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2)array[$i]=$(i+1)}
        {print array["docid"],array["volume"],array["subvolume"] }'
DATA

0
Reply Stu 2/15/2005 7:58:41 PM

Thanks but how is is the value referenced. I tried something liek this
printf ("%s\n", A[docid] ) and nothing printed.

0
Reply Stu 2/15/2005 8:05:50 PM

In article <1108497950.689102.85870@g14g2000cwa.googlegroups.com>,
Stu <beefstu350@hotmail.com> wrote:
>Thanks but how is is the value referenced. I tried something liek this
>printf ("%s\n", A[docid] ) and nothing printed.
>

    printf ("%s\n", A["docid"] )

Array indices are strings.  You could also do:

    docid = "docid"
    printf ("%s\n", A[docid] )

0
Reply gazelle 2/15/2005 8:08:12 PM

Thanks, works like a champ !

0
Reply Stu 2/15/2005 8:31:12 PM


Stu wrote:
> Ed,
> 
> The following did not print any output.
> 
> nawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2)array[$i]=$(i+1)}
>         {print array["docid"],array["volume"],array["subvolume"] }'
> DATA
> 

I didn't use nawk, I used gawk (see my original post). I can't think of 
any reason to use nawk.

	Ed.
0
Reply Ed 2/15/2005 9:10:28 PM


Kenny McCormack wrote:

> In article <cutjib$ii6@netnews.proxy.lucent.com>,
> Ed Morton  <morton@lsupcaemnt.com> wrote:
<snip>
>>
>>gawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2}array[$i]=$(i+1)}
>>	{print array["docid"],array["volume"],array["subvolume"] }' file
> 
> 
> Isn't that a wee tad more difficult than it ought to be?

I don't think so. This:

gawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2}A[$i]=$(i+1)}'

doesn't seem any more difficult in any way than this:

gawk '{for (i=1; i<=NF; i++)A[T[1]] = T[split($i,T,"=")]}'

I can understand my version at a glance (yes, I know, I wrote it...). 
I'd really have to think about yours to figure out what it's doing. I 
suspect we'll have to agree to differ on this one.

	Ed.
0
Reply Ed 2/15/2005 9:17:22 PM

Ed Morton wrote:

> Try this:
>
> gawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2}array[$i]=$(i+1)}
> 	{print array["docid"],array["volume"],array["subvolume"] }' file
>
> Regards,
>
> 	Ed.

The first parenthesis is unmatched.

Don't ask others to try what you haven't tried yourself.

0
Reply William 2/15/2005 10:54:05 PM

Stu wrote:
> All,
>
> I have the the follwing input:
>
> docid=00000850 volume=0006 subvolume=0026
> docid=00000851 volume=0046 subvolume=0006
> docid=00000852 volume=0036 subvolume=0086
>
> What I want am looking to do is to parse the name values pairs
> into an array with out being depandant on position of the input.
>


Simple, standard Awk (remove the "* " at the start of
each line; it's needed to protect indentation from google):

* BEGIN { FS = "=|[ \t]+" }
* { for (i=1; i<NF; i+=2)
*     array[$i] = $(i+1)
*   print array["docid"],array["volume"],array["subvolume"]
* }

Since FS should be the same every time the program is
run, it is not good to set in on the command line.

It is better to put the Awk program in a file by itself
than to create a file that is a mish-mash of bash and
Awk.

0
Reply William 2/15/2005 11:03:27 PM

Kenny McCormack wrote:

> {
> for (i=1; i<=NF; i++)
>     A[T[1]] = T[split($i,T,"=")]
> }

Extremely clever.  As soon as I fully comprehend it,
I will learn something from it.

0
Reply William 2/15/2005 11:05:32 PM


William James wrote:
> Ed Morton wrote:
> 
> 
>>Try this:
>>
>>gawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2}array[$i]=$(i+1)}
>>	{print array["docid"],array["volume"],array["subvolume"] }' file
>>
>>Regards,
>>
>>	Ed.
> 
> 
> The first parenthesis is unmatched.
> 
> Don't ask others to try what you haven't tried yourself.
> 

Sigh, here we go again. Just when I thought you'd decided to play nice....

People posting questions to NGs are typically fairly bright people just 
looking for guidance, not babies asking to be spoonfed. I really don't 
think fixing a small syntax error is beyond the average poster, nor do I 
think it's a particularly bad thing for them to have to think about the 
solution a little.

	Ed.
0
Reply Ed 2/15/2005 11:30:38 PM

Stu wrote:
> Ed,
> The following did not print any output.
> nawk -F"[=[:blank:]]" '{for (i=1;i<NF;i+=2)array[$i]=$(i+1)}
>         {print array["docid"],array["volume"],array["subvolume"] }'
> DATA

Solaris?
Try using /usr/xpg4/bin/awk..

0
Reply gerryt2 2/15/2005 11:56:01 PM

In article <1108508607.334533.40050@l41g2000cwc.googlegroups.com>,
William James <w_a_x_man@yahoo.com> wrote:
....
>It is better to put the Awk program in a file by itself
>than to create a file that is a mish-mash of bash and
>Awk.

Agree 100%.  That's why I always post my solutions as pure AWK (that being
the name of the game around here), without the attendant shell wrapper
nonsense.

0
Reply gazelle 2/16/2005 12:01:33 AM

In article <1108508732.665767.216940@f14g2000cwb.googlegroups.com>,
William James <w_a_x_man@yahoo.com> wrote:
>
>Kenny McCormack wrote:
>
>> {
>> for (i=1; i<=NF; i++)
>>     A[T[1]] = T[split($i,T,"=")]
>> }
>
>Extremely clever.  As soon as I fully comprehend it,
>I will learn something from it.

Thank you.  Of course, it I weren't playing golf, I'd have written it as:

{
for (i=1; i<=NF; i++) {
	split($i,T,"=")
	A[T[1]] = T[2]
	}
}

So as not to rely upon the RHS being evaluated before the LHS (which seems
reliable enough in every AWK I've ever used - but I wouldn't reply upon it
in C, of course)

0
Reply gazelle 2/20/2005 6:50:43 PM

15 Replies
761 Views

(page loaded in 0.116 seconds)

Similiar Articles:


















7/19/2012 9:01:40 PM


Reply: