Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a camelCase string into an array in awk?

How can I split a camelCase string into an array in awk using the split function?

Input:

STRING="camelCasedExample"

Desired Result:

WORDS[1]="camel"
WORDS[2]="Cased"
WORDS[3]="Example"

Bad Attempt:

split(STRING, WORDS, /([a-z])([A-Z])/);

Bad Result:

WORDS[1]="came"
WORDS[2]="ase"
WORDS[3]="xample"
like image 428
Raven Avatar asked May 14 '26 17:05

Raven


1 Answers

You can't do it with split() alone which is why GNU awk has patsplit():

$ awk 'BEGIN {
    patsplit("camelCasedExample",words,/(^|[[:upper:]])[[:lower:]]+/)
    for ( i in words ) print words[i]
}'
camel
Cased
Example
like image 149
Ed Morton Avatar answered May 16 '26 09:05

Ed Morton