1. field extraction ala awk

Hello,

Is there a straightforward way to extract fields delimited by say a colon or a comma, from a record, like it is done in AWK? It would be nice to replace my awk scripts by OEU scripts, if it was possible. smile

Thank you for suggestions.

new topic     » topic index » view message » categorize

2. Re: field extraction ala awk

sash said...

Hello,

Is there a straightforward way to extract fields delimited by say a colon or a comma, from a record, like it is done in AWK? It would be nice to replace my awk scripts by OEU scripts, if it was possible. smile

Thank you for suggestions.

In sequence.e there is split() and split_any(). One of these may suit your requirements. The source has examples, eg:

Example 1:

 result = split("John Middle Doe") 
 -- result is {"John", "Middle", "Doe"} 
 

Example 2:

 result = split("John,Middle,Doe", ",",, 2) -- Only want 2 sub-sequences. 
 -- result is {"John", "Middle,Doe"} 
 

Example 3:

 result = split("John||Middle||Doe|", '|') -- Each '|' is significant by default 
 -- result is {"John","","Middle","","Doe",""} 
 result = split("John||Middle||Doe|", '|', 1) -- Adjacent '|' are just a single delim, 
                                              -- and leading/trailing '|' ignored. 
 -- result is {"John","Middle","Doe"} 
 

Spock

new topic     » goto parent     » topic index » view message » categorize

3. Re: field extraction ala awk

Ah! Something for me to chew on.

Thank you! Live Long and Prosper!

new topic     » goto parent     » topic index » view message » categorize

4. Re: field extraction ala awk

I think keyvalues may suit your needs better. You can use it to chop up key/value pairs or simple delimited lists, where you need to account for quotes, white space, etc. It's really quite versatile!

Here's a contrived example I threw together:

 
include std/io.e 
include std/text.e 
include std/types.e 
include std/utils.e 
 
procedure print_pairs( integer fn, sequence data, integer haskeys = 1 ) 
 
    -- do not use white space as a delimiter for data with keys 
    sequence whitespace = iff( haskeys, "", " \t\r\n" ) 
    sequence pairs = keyvalues( data,,,, whitespace, haskeys ) 
 
    for i = 1 to length( pairs ) do 
 
        if haskeys then 
            sequence key = pairs[i][1] 
            sequence value = pairs[i][2] 
            printf( fn, "%s = %s\n", {key,value} ) 
        else 
            printf( fn, "%d: %s\n", {i,pairs[i]} ) 
        end if 
 
    end for 
 
end procedure 
 
print_pairs( STDOUT, "name=Homer Simpson; address=742 Evergreen Tr; city=Springfield; state=OR; country=USA;" ) 
 
-- name = Homer Simpson 
-- address = 742 Evergreen Tr 
-- city = Springfield 
-- state = OR 
-- country = USA 
 
print_pairs( STDOUT, "Homer Marge Bart Lisa Maggie Grampa \"Santa's Little Helper\" \"Snowball II\"", FALSE ) 
 
-- 1: Homer 
-- 2: Marge 
-- 3: Bart 
-- 4: Lisa 
-- 5: Maggie 
-- 6: Grampa 
-- 7: Santa's Little Helper 
-- 8: Snowball II 
 

-Greg

new topic     » goto parent     » topic index » view message » categorize

5. Re: field extraction ala awk

Thanks Greg! I have definitely some interesting reading to do here. Much new info to absorb smile

For instance, how would one extract fields into variables, from something like an /etc/passwd file? Or any file using same format.

Thanks again.

new topic     » goto parent     » topic index » view message » categorize

6. Re: field extraction ala awk

sash said...

For instance, how would one extract fields into variables, from something like an /etc/passwd file? Or any file using same format.

That's easy! In this case split() works perfectly fine. Here's another contrived example: https://openeuphoria.org/pastey/299.wc

Edit: here are some helpful links:

-Greg

new topic     » goto parent     » topic index » view message » categorize

7. Re: field extraction ala awk

Great! Thanks again for being so helpful. I truly appreciate it. smile

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu