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: parsing name value pairs - comp.lang.awkAll, I have the the follwing input: docid=00000850 volume=0006 subvolume=0026 docid=00000851 volume=0046 subvolume=0006 docid=00000852 volume=0036 s... Street address parsing API - comp.lang.java.programmerDefine a variable at a fixed address? - comp.compilers.lcc ... parsing name value pairs - comp.lang.awk... similar kind and I need to extract a fixed ... Street address ... Looking for To Do solution - comp.databases.filemakerparsing name value pairs - comp.lang.awk Looking for To Do solution - comp.databases.filemaker parsing name value pairs - comp.lang.awk Looking for To Do solution - comp ... Parsing file names with spaces - comp.lang.perl.misc... fields by name - comp.lang.awk Hi I have a text file with fields in the format ... you'll need to make sure no other fields have spaces in ... parsing name value pairs ... how can i define ae title to my pacs - comp.protocols.dicom ...Or is there a #pragma for lcc that will let me define my own custom sections and place this variable in ... parsing name value pairs - comp.lang.awk... similar kind and I ... access variable by string name - comp.soft-sys.matlabparsing name value pairs - comp.lang.awk Do i = 1 By 1 While( valstem <> "") Parse Value valstem ... access variable by string name - comp.soft-sys.matlab ... are the ... awk behavior with tab separated file - comp.lang.awkparsing name value pairs - comp.lang.awk awk behavior with tab separated file - comp.lang.awk This makes it impossible to refer to fields by value ... awk, extracting ... IP Address as Common Name and HostNameVerifier? - comp.lang.java ...EXCLUDE_FILE(myvar.o).bss) *(.sbss) *(COMMON ... parsing name value pairs - comp.lang.awk... similar ... Definition of a Static IP Address | eHow.com Every computer ... define-generic-mode: how to control tab indentation? - comp.emacs ...One thing to do might be to go back until you find a known point and then invoke parse-partial-sexp from there. That might tell you something about the syntactic context ... Splitting into a multi-dimensional array - comp.lang.javascript ...parsing name value pairs - comp.lang.awk Splitting into a multi-dimensional array - comp.lang.javascript ..... value[0][0] = lang value[0][1] = 1 value[1][0] = name value ... json functions - comp.lang.xharbourparsing name value pairs - comp.lang.awk Parsing Tcl lists... in JavaScript - comp.lang.tcl JSON key from a variable? - comp ... Library functions to parse configuration ... Matching parenthesis in files - comp.lang.awkparsing name value pairs - comp.lang.awk... volume"],array["subvolume"] }' file ... volume"],array["subvolume"] }' file > > Regards, > > Ed. The first parenthesis is ... Bareword errors? - comp.lang.perl.misc... use strict; my $search; my %HASH; my $name; my $value; my @pairs ... soon as they see a piece of trivial CGI parsing ... buffer ) { my ($name, $value ... CSV files with some double-quoted fields with commas inside - comp ...I've seen some posts offering ways to parse this, but ... awk, to convert from CSV format to a tab-separated-value format (which I'm calling TSV for want of a better name ... convert integer to string - comp.lang.perl.misc... element in my anonymous hash, concatenate each name ... some incoming data? that is a no-no! do a proper parse ... will put the list of courses into the hash as key/value pairs ... regex - Regular expression for parsing name value pairs - Stack ...Can someone provide a regular expression for parsing name/value pairs from a string? The pairs are separated by commas, and the value can optionally be enclosed in ... How to parse key/value pairs in C# - O'Reilly AnswersHow to parse key/value pairs in C#. How to make Azure talk to ... Key/value pairs in the wild. Honolulu is the newest ... there's only a comma between the city name and ... 7/19/2012 9:01:40 PM
|