-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextpipe.py
More file actions
371 lines (301 loc) · 12.4 KB
/
textpipe.py
File metadata and controls
371 lines (301 loc) · 12.4 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env python3
"""
textpipe -- Chain text operations from the command line. Cross-platform grep+awk+sort+uniq.
No need for grep | awk | sort | uniq | head pipelines. One tool, ordered operations.
Works on Windows, macOS, Linux. Zero dependencies.
Usage:
py textpipe.py input.txt grep "ERROR" # grep for ERROR
py textpipe.py input.txt grep "ERROR" count # count ERROR lines
py textpipe.py access.log fields 7 freq head 10 # top 10 URLs
py textpipe.py data.txt replace "foo" "bar" upper # replace then uppercase
py textpipe.py log.txt grep "4\\d\\d" extract "\\d{3}" freq # HTTP error code frequency
py textpipe.py names.txt sort unique number # sort, dedup, number
py textpipe.py data.csv delim "," fields 2,3 trim # extract CSV columns
cat file.txt | py textpipe.py grep "TODO" upper # stdin piping
echo "hello world" | py textpipe.py upper # quick transform
Operations (applied LEFT to RIGHT):
grep PATTERN Keep lines matching regex
grepv PATTERN Keep lines NOT matching regex
replace OLD NEW Regex search-and-replace
extract PATTERN Extract regex matches (group 1 if captured, else full match)
fields N[,M,...] Select fields (1-indexed). e.g. fields 1,3,5
delim CHAR Set field delimiter (default: whitespace split)
unique Remove duplicate lines (preserves order)
sort Sort lines alphabetically
sortnum Sort lines numerically (by first number found)
reverse Reverse line order
head N Keep first N lines
tail N Keep last N lines
count Print line count
freq Frequency table (most common first)
upper Uppercase all lines
lower Lowercase all lines
trim Strip leading/trailing whitespace
squeeze Collapse consecutive blank lines to one
number Add line numbers
length Show each line's character length
prepend TEXT Add text before each line
append TEXT Add text after each line
between S E Keep lines between patterns S and E (inclusive)
sum Sum all numbers found in lines
tally Count lines (alias for count)
join SEP Join all lines into one with separator
split CHAR Split each line on CHAR into multiple lines
"""
import re
import sys
from collections import OrderedDict
def op_grep(lines: list[str], pattern: str) -> list[str]:
rx = re.compile(pattern, re.IGNORECASE)
return [l for l in lines if rx.search(l)]
def op_grepv(lines: list[str], pattern: str) -> list[str]:
rx = re.compile(pattern, re.IGNORECASE)
return [l for l in lines if not rx.search(l)]
def op_replace(lines: list[str], old: str, new: str) -> list[str]:
rx = re.compile(old)
return [rx.sub(new, l) for l in lines]
def op_extract(lines: list[str], pattern: str) -> list[str]:
rx = re.compile(pattern)
results = []
for line in lines:
for m in rx.finditer(line):
if m.groups():
results.append(m.group(1))
else:
results.append(m.group(0))
return results
def op_fields(lines: list[str], spec: str, delimiter: str | None) -> list[str]:
indices = []
for part in spec.split(","):
part = part.strip()
if "-" in part:
a, b = part.split("-", 1)
a = int(a) if a else 1
b = int(b) if b else 999
indices.extend(range(a, b + 1))
else:
indices.append(int(part))
result = []
for line in lines:
if delimiter:
parts = line.split(delimiter)
else:
parts = line.split()
selected = []
for i in indices:
if 1 <= i <= len(parts):
selected.append(parts[i - 1])
result.append(" ".join(selected) if not delimiter else delimiter.join(selected))
return result
def op_unique(lines: list[str]) -> list[str]:
seen = OrderedDict()
for l in lines:
seen[l] = None
return list(seen.keys())
def op_sort(lines: list[str]) -> list[str]:
return sorted(lines)
def op_sortnum(lines: list[str]) -> list[str]:
def num_key(line):
m = re.search(r"-?\d+\.?\d*", line)
return float(m.group()) if m else 0
return sorted(lines, key=num_key)
def op_reverse(lines: list[str]) -> list[str]:
return list(reversed(lines))
def op_head(lines: list[str], n: int) -> list[str]:
return lines[:n]
def op_tail(lines: list[str], n: int) -> list[str]:
return lines[-n:] if n > 0 else []
def op_count(lines: list[str]) -> list[str]:
return [str(len(lines))]
def op_freq(lines: list[str]) -> list[str]:
counts: dict[str, int] = {}
for l in lines:
counts[l] = counts.get(l, 0) + 1
ranked = sorted(counts.items(), key=lambda x: -x[1])
return [f"{c:>6} {val}" for val, c in ranked]
def op_upper(lines: list[str]) -> list[str]:
return [l.upper() for l in lines]
def op_lower(lines: list[str]) -> list[str]:
return [l.lower() for l in lines]
def op_trim(lines: list[str]) -> list[str]:
return [l.strip() for l in lines]
def op_squeeze(lines: list[str]) -> list[str]:
result = []
prev_blank = False
for l in lines:
is_blank = l.strip() == ""
if is_blank and prev_blank:
continue
result.append(l)
prev_blank = is_blank
return result
def op_number(lines: list[str]) -> list[str]:
w = len(str(len(lines)))
return [f"{i+1:>{w}} {l}" for i, l in enumerate(lines)]
def op_length(lines: list[str]) -> list[str]:
return [f"{len(l):>5} {l}" for l in lines]
def op_prepend(lines: list[str], text: str) -> list[str]:
return [text + l for l in lines]
def op_append(lines: list[str], text: str) -> list[str]:
return [l + text for l in lines]
def op_between(lines: list[str], start: str, end: str) -> list[str]:
rx_start = re.compile(start)
rx_end = re.compile(end)
result = []
inside = False
for l in lines:
if not inside and rx_start.search(l):
inside = True
if inside:
result.append(l)
if inside and rx_end.search(l):
inside = False
return result
def op_sum(lines: list[str]) -> list[str]:
total = 0.0
for l in lines:
for m in re.finditer(r"-?\d+\.?\d*", l):
total += float(m.group())
if total == int(total):
return [str(int(total))]
return [f"{total:.4f}"]
def op_join(lines: list[str], sep: str) -> list[str]:
return [sep.join(lines)]
def op_split(lines: list[str], char: str) -> list[str]:
result = []
for l in lines:
result.extend(l.split(char))
return result
def parse_and_run(args: list[str]):
"""Parse args, read input, apply operations in order."""
if not args or args[0] in ("-h", "--help"):
print(__doc__.strip())
return
# Determine input source
idx = 0
input_file = None
# Check if first arg is a file or an operation
if args[0] not in get_op_names() and args[0] != "-":
input_file = args[0]
idx = 1
# Read input
if input_file and input_file != "-":
try:
with open(input_file, "r", encoding="utf-8", errors="replace") as f:
lines = [l.rstrip("\n\r") for l in f]
except FileNotFoundError:
print(f"Error: file not found: {input_file}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error reading {input_file}: {e}", file=sys.stderr)
sys.exit(1)
else:
if sys.stdin.isatty() and not input_file:
# No file, no pipe -- check if first arg might be a file
print("Error: no input. Provide a file or pipe stdin.", file=sys.stderr)
print("Usage: py textpipe.py <file> [operations...]", file=sys.stderr)
sys.exit(1)
lines = [l.rstrip("\n\r") for l in sys.stdin]
# Parse and apply operations in order
delimiter = None # for fields op
while idx < len(args):
op = args[idx].lower()
idx += 1
if op == "grep":
if idx >= len(args):
print("Error: grep requires a pattern", file=sys.stderr); sys.exit(1)
lines = op_grep(lines, args[idx]); idx += 1
elif op == "grepv":
if idx >= len(args):
print("Error: grepv requires a pattern", file=sys.stderr); sys.exit(1)
lines = op_grepv(lines, args[idx]); idx += 1
elif op == "replace":
if idx + 1 >= len(args):
print("Error: replace requires OLD NEW", file=sys.stderr); sys.exit(1)
lines = op_replace(lines, args[idx], args[idx + 1]); idx += 2
elif op == "extract":
if idx >= len(args):
print("Error: extract requires a pattern", file=sys.stderr); sys.exit(1)
lines = op_extract(lines, args[idx]); idx += 1
elif op == "fields":
if idx >= len(args):
print("Error: fields requires column spec (e.g. 1,3,5)", file=sys.stderr); sys.exit(1)
lines = op_fields(lines, args[idx], delimiter); idx += 1
elif op == "delim":
if idx >= len(args):
print("Error: delim requires a character", file=sys.stderr); sys.exit(1)
delimiter = args[idx]; idx += 1
elif op in ("unique", "uniq"):
lines = op_unique(lines)
elif op == "sort":
lines = op_sort(lines)
elif op == "sortnum":
lines = op_sortnum(lines)
elif op in ("reverse", "rev"):
lines = op_reverse(lines)
elif op == "head":
if idx >= len(args):
print("Error: head requires a number", file=sys.stderr); sys.exit(1)
lines = op_head(lines, int(args[idx])); idx += 1
elif op == "tail":
if idx >= len(args):
print("Error: tail requires a number", file=sys.stderr); sys.exit(1)
lines = op_tail(lines, int(args[idx])); idx += 1
elif op in ("count", "tally"):
lines = op_count(lines)
elif op == "freq":
lines = op_freq(lines)
elif op == "upper":
lines = op_upper(lines)
elif op == "lower":
lines = op_lower(lines)
elif op == "trim":
lines = op_trim(lines)
elif op == "squeeze":
lines = op_squeeze(lines)
elif op in ("number", "nl"):
lines = op_number(lines)
elif op in ("length", "len"):
lines = op_length(lines)
elif op == "prepend":
if idx >= len(args):
print("Error: prepend requires text", file=sys.stderr); sys.exit(1)
lines = op_prepend(lines, args[idx]); idx += 1
elif op == "append":
if idx >= len(args):
print("Error: append requires text", file=sys.stderr); sys.exit(1)
lines = op_append(lines, args[idx]); idx += 1
elif op == "between":
if idx + 1 >= len(args):
print("Error: between requires START END patterns", file=sys.stderr); sys.exit(1)
lines = op_between(lines, args[idx], args[idx + 1]); idx += 2
elif op == "sum":
lines = op_sum(lines)
elif op == "join":
if idx >= len(args):
print("Error: join requires a separator", file=sys.stderr); sys.exit(1)
lines = op_join(lines, args[idx]); idx += 1
elif op == "split":
if idx >= len(args):
print("Error: split requires a character", file=sys.stderr); sys.exit(1)
lines = op_split(lines, args[idx]); idx += 1
else:
print(f"Error: unknown operation '{op}'", file=sys.stderr)
print(f"Operations: {', '.join(get_op_names())}", file=sys.stderr)
sys.exit(1)
# Output
for line in lines:
print(line)
def get_op_names() -> set[str]:
return {
"grep", "grepv", "replace", "extract", "fields", "delim",
"unique", "uniq", "sort", "sortnum", "reverse", "rev",
"head", "tail", "count", "tally", "freq",
"upper", "lower", "trim", "squeeze",
"number", "nl", "length", "len",
"prepend", "append", "between", "sum", "join", "split",
}
def main():
parse_and_run(sys.argv[1:])
if __name__ == "__main__":
main()