-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find and Replace Columns.tmCommand
67 lines (64 loc) · 1.68 KB
/
Find and Replace Columns.tmCommand
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/ruby -w
# This is designed to work with a tab delimited document where
# you need to do a find/replace on each col & row value. This is
# useful if you're trying to import database records and need to
# match up names with id numbers.
#
# To get this to work, you need to copy a "key" into your pasteboard.
# The first column is the replacement text, and every column after that
# is the find text. It does an exact match.
#
# Pasteboard:
# 1 apple apples
# 2 banana naner
#
# Given:
# apple naner
# apples bananana
# appleish banana
#
# Result:
# 1 2
# 1 bananana
# applish 2
class MultiReplace
def initialize(pattern)
@pattern = {}
pattern.split("\n").each do |row|
row = row.split("\t")
matcher = row.first
row.each do |column|
@pattern[column] = matcher unless column == matcher
end
end
end
def replace(text)
rows = text.split("\n").map { |r| r.split("\t") }
rows.map do |row|
row.map do |column|
@pattern[column] || column
end.join("\t")
end.join("\n")
end
end
print MultiReplace.new(`pbpaste`).replace(STDIN.read)
</string>
<key>input</key>
<string>selection</string>
<key>name</key>
<string>Find and Replace Columns</string>
<key>output</key>
<string>replaceSelectedText</string>
<key>scope</key>
<string>text</string>
<key>uuid</key>
<string>EB8BDD64-CB27-43D7-B173-A18A3417F916</string>
</dict>
</plist>