Search code examples
openrefine

Delete everything before a character on certain lines in a large text in OpenRefine


I’ve looked around but did not find an answer.

I’m cleaning a large amount of texts in OpenRefine. What I am trying to do is to suppress lines—between two end of lines (\n)—containing a specific character—in this case %. It looks like this:

...En trois mots, la bouffe lyonnaise, ça se résume à quoi?\n« Réconfortante, savoureuse, chaleureuse. » \n \nLa quenelle de brochet et sa sauce aux écrevisses %\nL'extra avec ça?\nLe chef Viola concoctera une soupe géante et celle-ci sera partagée GRATUITEMENT le samedi 25 février 2017! Stay tuned! \nLe bouchon lyonnais du Balmoral, c'est un rendez-vous! \nMontréal en Lumière - volet gastronomie\n23 février au 11 mars 2016 \nLE BALMORAL\n514 288-5992

I am looking for such result (without the bolded line):

...En trois mots, la bouffe lyonnaise, ça se résume à quoi?\n« Réconfortante, savoureuse, chaleureuse. » \n \n\nL'extra avec ça?\nLe chef Viola concoctera une soupe géante et celle-ci sera partagée GRATUITEMENT le samedi 25 février 2017! Stay tuned! \nLe bouchon lyonnais du Balmoral, c'est un rendez-vous! \nMontréal en Lumière - volet gastronomie\n23 février au 11 mars 2016 \nLE BALMORAL\n514 288-5992

This, for many instances in multiple texts.

Help would be greatly appreciated.


Solution

  • I'm not sure whether the "\n" are literal or a representation of the LF character, but I'll assume the former and you can adjust the formula, if necessary. The solution involves splitting the lines, iterating through the lines and filtering the lines containing '%' and joining the lines again. Use the following formula in the "Edit Cells -> Transform" dialog:

    forEach(value.split('\\n'),l,if(l.contains('%'),'',l)).join('\\n')

    To break it down:

    • value.split('\\n') yields an array of split lines
    • forEach(array,l,f) iterates through the array assigning each line to the variable l and applying function f
    • if(l.contains('%'),'',l)) returns the empty string if l contains a percent ('%') otherwise the original string
    • array.join('\\n') joins your filtered lines back together again