How to join multi-valued cells with a character followed by a line break?

564 views
Skip to first unread message

Gorav Seth

unread,
Feb 27, 2014, 2:00:02 PM2/27/14
to openr...@googlegroups.com
I have what is hopefully a simple question.  I need to join multi-valued cells, with the separator being a semi-colon and a line break.

I dont believe I can include the line break directly when I do the join, so I'm trying to join w/ a comma, and then use "add column based on this column" and replacing the comma w/ a semicolon and a line break.

However, I cannot figure out how to replace the character w/ a semicolon plus a line break in GREL.  It seems that value.replace lets me match using regex, but I can only replace w/ a string, not a line break.  Is there a better way to go about this in GREL?  Should I try clojure or jython?

Thanks
Gorav


Gorav Seth

unread,
Mar 9, 2014, 3:09:53 PM3/9/14
to openr...@googlegroups.com
This is easily done in excel, using find/replace.  

Does anyone know of a way to include a line break within a replace function in Refine?

Thanks again,
Gorav

Thad Guidry

unread,
Mar 9, 2014, 10:24:48 PM3/9/14
to openr...@googlegroups.com
Use Regular Expressions if need be, in Java style, \r\n or \n

value.replace('make_this__string_line_break',/\n/)

value.replace(/\s+/, /\r\n/)



--
You received this message because you are subscribed to the Google Groups "OpenRefine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openrefine+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Gorav Seth

unread,
Mar 29, 2017, 11:44:39 AM3/29/17
to OpenRefine


I'm doing this again after a long time (replace semicolon with line break using add column based on this column) and finding that using value.replace with regex throws an error, even though the expression box says 'no syntax error' 


Without regex, there is no issue.  ie value.replace(';', 'abcd') works fine


however, If I try value.replace(';', /\n/), the expression box indicates no syntax error, but the output is an error : 


Error: replace expects 3 strings, or 1 string, 1 regex, and 1 string.


I'm using openrefine v 2.7 rc2


Screenshot below.  Any help appreciated!  


Owen Stephens

unread,
Mar 29, 2017, 12:01:10 PM3/29/17
to OpenRefine
'replace' only takes a regular expression for the 'search' bit of the expression.
However - if you put \n in a literal string it will treat it as a linebreak, so you can use:

value.replace(/\s/,"\n")

Should do what you want.
You could also use

value.split(/\s/,"\n")

Also note that in the preview and UI the line breaks don't display, but if you click 'Edit' in a cell you should see the line breaks are in there

Owen

Gorav Seth

unread,
Mar 29, 2017, 12:20:17 PM3/29/17
to OpenRefine
thanks!!!  

edit was key, and putting \n in as a literal string.

for the record, what worked for me was

value.replace(';',"\n")

value.split resulted in an error : String[] value not storable

and value.replace(/\s/,"\n") appeared to split at each space, not just at semicolons

thanks a ton. 

Owen Stephens

unread,
Mar 29, 2017, 1:10:55 PM3/29/17
to openr...@googlegroups.com
"split" didn't work because I completely typed the wrong thing!

Should have been:

value.split(/\s/).join("\n")

Of course change the expression in split to a semi colon or whatever 

Owen 
Reply all
Reply to author
Forward
0 new messages