Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Explanation

With the help of REGEX I am trying to convert italian synonym database to the following format:

synonym1|synonym2|synonym3
anothersynonym1|anothersynonym2
...........
......

At this moment I have unconverted synonyms database in this format:

a|       - word
|per     -it's synonym
|verso   -another synonym of the word "a" in the second row
abate|    - second word
|priore|superiore
abbacchiare|
|avvilire|deprimere
abbacchiarsi|
|abbattersi|abbiosciarsi|accasciarsi

Clarification: I would like to use Regular Expression to match all lines beginning with "|" (pipeline) and move them to the line above to merge with the word matching with lines ending with (Pipeline) and also moving in rows beginning with "|" (second - third - fourth lines of synonyms which may be merged together, obviously when merged lines with "ending and beginning" Pipelines one of the (Pipelines) should removed to get desired format:

Example of the converted database from unconverted database above

a|per|verso
abate|priore|superiore
abbachiare|avvilire|deprimere
abbachiarsi|abbattersi|abbiosciarsi|accasciarsi

Can anybody help me to find exact REGULAR EXPRESSION by using function FIND and REPLACE with any text editor?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

If you are using Windows you can use the Extended Search Mode of Notepad++ to do this in 2 steps:

1: Replace |\r\n| with | - This will find all of the first synonyms

first replace

a|per
|verso
abate|priore|superiore
abbacchiare|avvilire|deprimere
abbacchiarsi|abbattersi|abbiosciarsi|accasciarsi

2: Replace \r\n| with | - This will find all of the subsequent synonyms

second replace

a|per|verso
abate|priore|superiore
abbacchiare|avvilire|deprimere
abbacchiarsi|abbattersi|abbiosciarsi|accasciarsi
share|improve this answer
    
Thank you very , very much , very descriptive even with screenshots , your solution worked perfectly exactly how you wrote !!! –  Daniel Foltynek Oct 19 '11 at 13:04
perl -ne 'chomp; print /^(.*)\|\s*$/?"\n$1":$_' filename

a|per|verso
abate|priore|superiore
abbacchiare|avvilire|deprimere
abbacchiarsi|abbattersi|abbiosciarsi|accasciarsi

Or, if you are fussy about leading and trailing newlines:

perl -ne 'chomp; print /^(.*)\|\s*$/?$.==1?$1:"\n$1":$_;END{print"\n"}' filename
a|per|verso
abate|priore|superiore
abbacchiare|avvilire|deprimere
abbacchiarsi|abbattersi|abbiosciarsi|accasciarsi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.