1. field extraction ala awk
- Posted by sash Feb 24, 2019
- 1150 views
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.
Thank you for suggestions.
2. Re: field extraction ala awk
- Posted by Spock Feb 24, 2019
- 1140 views
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.
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
3. Re: field extraction ala awk
- Posted by sash Feb 24, 2019
- 1114 views
Ah! Something for me to chew on.
Thank you! Live Long and Prosper!
4. Re: field extraction ala awk
- Posted by ghaberek (admin) Feb 25, 2019
- 1104 views
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
5. Re: field extraction ala awk
- Posted by sash Feb 25, 2019
- 1084 views
Thanks Greg! I have definitely some interesting reading to do here. Much new info to absorb
For instance, how would one extract fields into variables, from something like an /etc/passwd file? Or any file using same format.
Thanks again.
6. Re: field extraction ala awk
- Posted by ghaberek (admin) Feb 25, 2019
- 1156 views
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:
- https://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/
- https://www.cyberciti.biz/faq/understanding-etcshadow-file/
- https://www.cyberciti.biz/faq/understanding-etcgroup-file/
-Greg
7. Re: field extraction ala awk
- Posted by sash Feb 25, 2019
- 1096 views
Great! Thanks again for being so helpful. I truly appreciate it.