sed: Support single pattern addresses.
This commit is contained in:
parent
194c098ab7
commit
1302c8bf28
1
check.sh
1
check.sh
|
@ -144,6 +144,7 @@ tests='
|
|||
100-sed-undo
|
||||
100-sed-file
|
||||
100-sed-fooRbar
|
||||
100-sed-pattern-address
|
||||
100-sed-autoconf-dirname
|
||||
|
||||
100-tar
|
||||
|
|
|
@ -97,6 +97,14 @@
|
|||
((and m+ (_ _ ...)) (proc str m+))
|
||||
(_ str))))
|
||||
|
||||
(define (address->pred address)
|
||||
(if (string? address)
|
||||
(let* ((flags `(,(if (extended?) regexp/extended regexp/basic)))
|
||||
(pattern (replace-escapes address))
|
||||
(regexp (apply make-regexp pattern flags)))
|
||||
(cut regexp-exec regexp <>))
|
||||
(error "SED: unsupported address type" address)))
|
||||
|
||||
(define (execute-function function str)
|
||||
(match function
|
||||
(('s pattern replacement flags)
|
||||
|
@ -108,6 +116,12 @@
|
|||
(() str)
|
||||
((('always function) . rest)
|
||||
(execute-commands rest (execute-function function str)))
|
||||
((('at address function) . rest)
|
||||
;; XXX: This should be "compiled" ahead of time so that it only
|
||||
;; runs once intead of once per line.
|
||||
(if ((address->pred address) str)
|
||||
(execute-commands rest (execute-function function str))
|
||||
(execute-commands rest str)))
|
||||
((cmd . rest) (error "SED: could not process command" cmd))))
|
||||
|
||||
(define* (edit-stream commands #:optional
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
input='bar
|
||||
baz
|
||||
bam'
|
||||
|
||||
echo "$input" | \sed '/baz/ s/a/i/'
|
|
@ -0,0 +1,3 @@
|
|||
bar
|
||||
biz
|
||||
bam
|
Loading…
Reference in New Issue