Scripting questions.

14 views
Skip to first unread message

alan101

unread,
Sep 16, 2008, 8:19:39 PM9/16/08
to TextWrangler Talk
I'm quite new to TW, having recently moved to Mac from Windows and
SlickEdit. Have read manual and googled without success. Please help
with a couple of scripting-type questions.

1) I would like to sort lines based on column/block selection. Imagine
10 lines, each 80 characters in length. I want to sort all 10 lines of
80 characters, using columns 12-20 as the sort key. Put another way:
sort -key=1.12,1.20 <filename>.

Using a selection with the included sort scripts means that columns
1-11 and 21-80 remain fixed in place, while columns 12-20 are replaced
with the sorted selection. The slickedit equivalent is 'sort-on-
selection'

I could call a script to use the OS-X sort, but how would I pass the
column start/end without actually selecting the columns?

2) "Paste column" doesn't paste the way I want. If lines following the
position you have chosen for the insertion are shorter than the column
of the insertion point, the text for those lines moves left to the end-
of-line. The paste is then jagged instead of retaining its "block"
shape. If I add spaces to all lines so that the lines are at least
as many columns as the insertion point, the "paste column" inserts a
perfect block.

One workaround would be to write a script that inserts spaces at the
end of all lines up to the maximum column required.



These questions were a little difficult to explain without screen
shots. I hope you can make sense of it all. I'd appreciate any help
from the long-time wranglers. While I was a long-time user of
SlickEdit, I just can't handle the horrible native X interface it uses
on OS-X. Better to take the pain of learning a new editor and
rewriting the library of macros/scripts.

As a minor contribution, here is a script that aligns text in
columns. It's easier to see than explain. Type text into a TW
windows - something like:

3d wd 100 d wed w de wed w dw
wedwedw wed 29002 wedwedwed wedweddw
wdew wd 1 wd wed wd w ed wed

Use shebang to run the script and see what you get. Note that the
numeric columns are left-aligned while the text columns are right-
aligned. Not perhaps immediately useful to you, but I use it often.
I'm not a pro, so pardon any inefficient Python code. I named it
'align.py'.

import os, sys, string

ll = []
num = []

if len(sys.argv) != 2:
print 'Syntax: align <input filename>'
print ' output is to stdout.'
sys.exit(1)

try:
InFile=file(sys.argv[1], 'r')
except:
print 'Input file not found or locked'
sys.exit(1)

ln = InFile.readline()
while ln <> "":
xl = string.split(ln)
for i in range(len(xl)):
if len(ll) <= i:
ll.append(len(xl[i]))
num.append('y')
else:
if ll[i] < len(xl[i]):
ll[i] = len(xl[i])
if num[i] == 'y' and xl[i].isdigit() == 0:
num[i] = 'n'
ln = InFile.readline()

InFile.close()


InFile=file(sys.argv[1], 'r')
ln = InFile.readline()
while ln <> "":

str = ''
cumlen = 0
xl = string.split(ln)
for i in range(len(xl)):
if num[i] == 'y':
str += xl[i].rjust(ll[i]) + ' '
else:
str += xl[i].ljust(ll[i]) + ' '
print str
ln = InFile.readline()

InFile.close()




Thanks,
Alan.


alan101

unread,
Sep 16, 2008, 9:24:22 PM9/16/08
to TextWrangler Talk
Oops. Don't know my left alignment from my right... It's of course
vice versa.

Tom Robinson

unread,
Sep 17, 2008, 9:31:15 AM9/17/08
to textwr...@googlegroups.com
On 2008-09-17, at 12:19, alan101 wrote:

> 1) I would like to sort lines based on column/block selection. Imagine
> 10 lines, each 80 characters in length. I want to sort all 10 lines of
> 80 characters, using columns 12-20 as the sort key

I suggest Text -> Sort Lines.

Select Sort using pattern, search pattern is below, sort using all sub-
patterns (or \1):

^.{11}(.{9})

i.e. Look for 11 characters starting at the beginning of the line,
pull the next 9 characters into sub-pattern 1.

Cheers

alan101

unread,
Sep 18, 2008, 7:45:32 AM9/18/08
to TextWrangler Talk
Tom, that is a seriously elegant approach. It totally blew my mind.
Thanks very much.

Cheers,
Alan.
Reply all
Reply to author
Forward
0 new messages