Manipulating, Aligning data without RegExp

Usage tips, posted by users. No questions here please.

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
g00ber
Posts: 10
Joined: Fri Jul 11, 2003 8:01 pm

Manipulating, Aligning data without RegExp

Post by g00ber »

When trying to do a Search or Replace, sometimes its hard to come up w/ RegExp, so what I do is use a combination of Replace and Block Select Mode.

Heres a simple example:

Lets say my goal is to align the second items in each line.

Code: Select all

banana, pears, apple
lemon, mango, pineapple, lime
avocado, lime, pepper, apple
green beans, lime, apple
lime, snowpeas, carrots
1) Press F8 (Replace)
Type "," (1 comma) in Find-What
Type ", " (1 comma then space) in Replace-With
Press Replace-All. Keep pressing this until you see each item being "pulled apart" from each other. See example below.

Code: Select all

banana,         pears,         apple 
lemon,         mango,         pineapple,         lime
avocado,         lime,         pepper,         apple 
green beans,         lime,         apple 
lime,         snowpeas,         carrots
2) Press SHIFT+F10 then B (Block Select Mode)
Highlight the columns beginning from the s in snowpeas up to the first line making sure that the spaces in front of pears (line1), mango (line2), lime (line3), lime (line4) are highlighted.

3) Press F8 (Replace)
Type " " (1 space) in Find-What
Type "" (nothing) in Replace-With
Press Replace-All. See example below.

Code: Select all

banana,       pears,         apple 
lemon,        mango,        pineapple,         lime
avocado,      lime,         pepper,         apple 
green beans,  lime,         apple 
lime,         snowpeas,         carrots
TADA!!

4) Now lets take this a step further. Our second goal is to replace all the words "lime" with "grapes" if they appear as the second item of each line.
While still in Block Select Mode, highlight all the columns covering all the words pears (line1), mango (line2), lime (line3), lime (line4), snowpeas (line5).
Press F8 (Replace)
Type "lime" in Find-What
Type "grapes" in Replace-With
Press Selected-text
Press Replace-All
See example below.

Code: Select all

banana,       pears,         apple 
lemon,        mango,        pineapple,         orange
avocado,      grapes,         pepper,         apple 
green beans,  grapes,         apple 
lime,         snowpeas,         carrots
TADA!!

I think the important thing to remember is finding a pattern in your data (in this case, it was the comma), then using that pattern to "pull apart" items by adding a space character, then further manipulating them.
Last edited by g00ber on Tue Aug 10, 2004 1:17 pm, edited 1 time in total.
User avatar
trids
Posts: 69
Joined: Wed May 07, 2003 10:16 am
Location: South Africa

Post by trids »

Nice Tip :wink: .. especially Step-1

You might want to try a slight deviation from Step 3, which I quite enjoy cos I'm so lazy: This is once you have selected the non-aligned columns in block mode ..

3) Press Shift-TAB to shift the selected block contents to the left, until the leftmost selected column aligns flush with the left of the selection block. You can also just TAB them to the right if you feel they are too far to the left at this point.

4) Select the next column to the right that is misaligned and return to step-3 ... until there are no more columns.

:idea:
User avatar
bbadmin
Site Admin
Posts: 879
Joined: Mon Feb 17, 2003 8:54 pm
Contact:

Post by bbadmin »

Another simple method is to right click the document, choose Properties, and set the default tab stop to a value greater than the longest word in any of the columns. If the columns were space separated, rather than tab separated, you can convert the spaces to tabs using replace all with the following regular expression:

Find what: + (that's <space><plus>)
Replace with: \t

If you also set the document property to convert tabs to spaces when saving, the file can be saved with the columns permanently reformatted.

Keith MacDonald
Helios Software Solutions
drefty
Posts: 35
Joined: Mon Dec 22, 2003 3:53 pm

line up delimiters

Post by drefty »

If you have Perl installed on your machine you could also do ...

1) Highlight the text in TextPad and Copy it to the windows clipboard;
2) run "LineUpDelims.pl" from a windows toolbar (this munges the clipboard text for you);
3) Paste the modified clipboard contents over the highlighted Text and you will have neatly lined-up text

Since lots of people have perl (or can get it free) this kind of solution obviates a lot of extra steps by using the windows clipboard as a 'transport' between Textpad and Perl.
Post Reply