Skip to content

Commit

Permalink
Make REPLACE accept any number of substitutions to perform in a singl…
Browse files Browse the repository at this point in the history
…e command.
  • Loading branch information
Scott Bronson authored and Scott Bronson committed May 22, 2008
1 parent 436aa12 commit 48a67f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGES
@@ -1,3 +1,4 @@
- Made REPLACE handle multiple text substitutions; could only handle 1 before.
- Got rid of RESULT and all its code. echo $? from within your test instead.
- Got rid of Cutest since it was very non-cute. Wrote zutest instead.
- Changed --all-files to be the much more understandable --ignore-extension.
Expand Down
44 changes: 13 additions & 31 deletions tmlib.sh
Expand Up @@ -229,12 +229,11 @@ MKDIR ()
INDENT ()
{
# sed appears more binary transparent than bash's builtins so I'm
# using sed instead of builtin read. It might even be faster.

# using it instead of builtin read. It might even be faster.
sed -e "s/^/${1- }/"

# even though this might be faster, it mucks things up.
#
# even though it would probably be faster to do it with the
# Bash built-in, the following mucks things up. Bash is sloppy.
# while read LINE; do
# echo $'\t'"$LINE"
# done
Expand All @@ -244,10 +243,13 @@ INDENT ()
#
# REPLACE
#
# Replaces all occurrences of the first argument with the second
# argument. Both should be regex-safe. Use sed if you want to
# replace with regexes. NOTE: replace does not work if a newline
# is embedded in either argument.
# Replaces all occurrences of the first argument with the second argument.
# Takes any number of arguments:
# REPLACE abc ABC def DEF ghi GHI
# converts the first nine characters of the alphabet to upper case
# All non-control characters are safe: quotes, slashes, etc.
# Use sed if you want to replace with regexes.
# NOTE: replace does not work if a newline is embedded in either argument.
#
# Three layers of escaping! (bash, perlvar, perlre) This is insane.
# I wish sed or awk would work with raw strings instead of regexes.
Expand All @@ -256,28 +258,8 @@ INDENT ()

REPLACE()
{
# unfortunately bash can't handle this substitution because it
# must work on ' and \ simultaneously. So, send it to perl for
# processing. Until ' and \ have been escaped, Perl can't


# echo "got: '$1' '${1//[\'\\]/\'}'"
# perl -e "print \"in: \" . quotemeta('${1//\'/\'}') . \"\\n\";"

# (echo "$1"; echo "$2"; cat) | cat
# (echo "$1"; echo "$2"; cat) | perl -e "my \$in = <>; chomp \$in; my \$out = <>; chomp \$out; print ' in: <<' . \$in . \">>\n out: <<\" . \$out . \">>\n\"; while(<>) { print \"DATA: \$_\" }"
# (echo "$1"; echo "$2"; cat) | perl -e "my \$in = <>; chomp \$in; \$in=quotemeta(\$in); my \$out = <>; chomp \$out; print ' in: <<' . \$in . \">>\n out: <<\" . \$out . \">>\n\"; while(<>) { print \"DATA: \$_\" }"

(echo "$1"; echo "$2"; cat) | perl -e "my \$in = <>; chomp \$in; \$in=quotemeta(\$in); my \$out = <>; chomp \$out; while(<>) { s/\$in/\$out/g; print or die \"Could not print: \$!\\\\n\"; }"
# unfortunately bash can't handle this substitution itself because it
# must work on ' and \ simultaneously. Send it to perl for processing.


# this scheme also works but it's much better to feed the vars on stdin
# along with the data. Less process overhead, simpler script. This
# does mean that REPLACE won't work with embedded newlines though.
#
# local in=$(perl -p -e "s/([\\'\\\\])/\\\\\$1/g" <<< $1);
# local out=$(perl -p -e "s/([\\'\\\\])/\\\\\$1/g" <<< $2);
#
# perl -e "<>; my \$in = quotemeta(chomp); <>; my \$out = chomp; while(<>) { s/\$in/\$out/g; print or die \"Could not print: \$!\\\\n\"; }"
# perl -e "my \$in = quotemeta('${1//\'/\'}'); my \$out = '${2//\'/\'}'; while(<>) { s/\$in/\$out/g; print or die \"Could not print: \$!\\\\n\"; }"
( while [ "$1" != "" ]; do echo "$1"; shift; done; echo; cat) | perl -e "my %ops; while(<>) { chomp; last if \$_ eq ''; \$_ = quotemeta(\$_); \$ops{\$_} = <>; chomp(\$ops{\$_}); warn 'odd number of arguments to REPLACE', last if \$ops{\$_} eq ''; } while(<>) { for my \$k (keys %ops) { s/\$k/\$ops{\$k}/g } print or die \"REPLACE: Could not print: \$!\\\\n\"; }"
}

0 comments on commit 48a67f5

Please sign in to comment.