Page 1 of 1

Can RE help me achieve ths?

Posted: Sat Oct 13, 2012 10:28 am
by terrypin
I have a TXT file which contains many entries like this. Note that some have 2 lines, others 3.

[LH+ZoomIn]
name=L Zin

[ReduceTo10percent]
name=Reduce to10%

[RH+ZoomIn]
name=R Zin

[34onL-Z-FlyDown]
name=34onL Z-FlyDown
text = This is 34onL-Z-FlyDown

[34CtoL]
name=34CtoL

[34CtoR]
name=34CtoR

[LineGrows-1]
name=LineGrows-1

[LineHorizRed]
name=LineHoriz Red

I want to create a new file as a result of sorting on the name but keeping the rest intact. So the above would look like this (done manually, so I may not be 100% correct about Win XP's sorting method):

[34CtoL]
name=34CtoL

[34CtoR]
name=34CtoR

[34onL-Z-FlyDown]
name=34onL Z-FlyDown
text = This is 34onL-Z-FlyDown

[LineGrows-1]
name=LineGrows-1

[LineHorizRed]
name=LineHoriz Red

[ReduceTo10percent]
name=Reduce to10%

[LH+ZoomIn]
name=L Zin

[RH+ZoomIn]
name=R Zin

Is there some clever way I can use RE to help do this in a few steps please?

--
Terry, East Grinstead, UK

Posted: Sat Oct 13, 2012 1:24 pm
by ak47wong
Here's one way to do it. The idea is to collapse each multi-line entry into a single line with the name at the front, sort the file, and then expand the entries into their original format.

1. Enable POSIX regular expression syntax in Configure > Preferences > Editor.

2. Collapse 3-line entries into single lines. Perform a Replace operation as follows:

Find what: (\[.*\])\n(name.*)\n(text.*)\n
Replace with: \2\1\3

Select Regular expression and click Replace All.

3. Collapse 2-line entries into single lines:

Find what: (\[.*\])\n(name.*)\n
Replace with: \2\1

4. Click Tools > Sort and sort the file as desired.

5. Expand 2-line entries to their original format:

Find what: ([^[]*)(\[[^]]*\])$
Replace with: \2\n\1\n

6. Expand 3-line entries to their original format:

Find what: ([^[]*)(\[[^]]*\])(.+)
Replace with: \2\n\1\n\3\n

Posted: Mon Oct 15, 2012 8:37 am
by terrypin
Thank you, much appreciated! I'll try that as soon as possible.

--
Terry, UK