Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > sepsplit - spli...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 2088 of 2297
Post > Topic >>

sepsplit - splitting into fields together with matched field

by Steffen Schuler <schuler.steffen@[EMAIL PROTECTED] > Dec 2, 2007 at 12:00 AM

Hello netlanders,

is there a faster POSIX awk function solving task SEPSPLIT below?



task SEPSPLIT

searched: 

function sepsplit(re, a, s) in POSIX awk with:
s is split into the fields a[0], a[2], ...,a[2n] by the field separator 
regex re and a[2*i+1] is the string matched by re between a[2*i] and 
a[2*i+2] for i = 0, 1, ..., n - 1. sepsplit() returns n.

solution:

function sepsplit(re, a, s,   suffix, m) {
  m = 0
  suffix = s
  while (match(suffix, re)) {
    a[m++] = substr(suffix, 1, RSTART - 1)
    a[m++] = substr(suffix, RSTART, RLENGTH)
    suffix = substr(suffix, RSTART + RLENGTH)
  }
  a[m] = suffix
  return m / 2 
}



remark:

there is also gawk solution with gensub() and index() possible.



Any useful answer is appreciated.

Regards,

Steffen "goedel" Schuler
 




 3 Posts in Topic:
sepsplit - splitting into fields together with matched field
Steffen Schuler <schul  2007-12-02 00:00:31 
Re: sepsplit - splitting into fields together with matched field
William James <w_a_x_m  2007-12-02 03:35:55 
Re: sepsplit - splitting into fields together with matched field
Steffen Schuler <schul  2007-12-04 22:41:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Jul 6 20:46:37 CDT 2008.