Friday, June 30, 2017

Recipe - Removing the first or last terms from a delimited string

Given the following sample data:


If I wanted to remove the first delimited term from each row (e.g. "Santa Cruz Beach Boardwalk" from row 1 and "Surfing" from all the other rows, I would use the following recipe:

Edit cells->Transform













and the following GREL expression:

value.split("<delimiter>").slice(1).join("<delimiter>")

where <delimiter> is whatever delimiter is being used for your data string.




















Alternatively, if you wanted to remove the last delimited term (in this case, "Santa Cruz"),
you would do the transform as above, but you would use this GREL expression:
value.split("<delimiter>").slice(0,length(value.split("<delimiter>"))-1.join("<delimiter>")

The -1 is needed because of the way Open Refine indexes an array. (I'll cover arrays and indexing in a future post)




No comments: