当我需要对不想打开进行编辑的 ASCII 文件进行一些小处理时,我发现自己在代码中使用了很多 Perl 单行正则表达式。
是否有与此 Perl 单行命令等效的 Tcl?
perl -i.bak -pe 's/old/new/gi' filename
查看 Richard Suchenwirth http://wiki.tcl.tk/906的 owh 脚本,它提供了这种功能。约阿希姆
我曾经写过一个 Tcl 脚本来提供类似 Perl 的命令行选项。我自己从来没有经常使用它——它最终成为了将代码发送到 Tcl interp 的练习。如果您有兴趣,代码位于https://bitbucket.org/glenn_j/tcl-command-line-one-liners/src
我使用此脚本在文件上就地执行shell 管道,但您可以更改commands="$1"
为printf '%s' "$1" > sometemporaryfile
和.sh -c "$commands"
tclsh sometemporaryfile
#!/bin/sh -e
# =====================================================
# filter file(s) without having to use a temporary file
# =====================================================
# EXAMPLE - remove first line and b-tags:
# in-place 'tail -n +2 | sed "s/<[Bb]>//g"' *.html *.htm
# # check that the commands did what you intended
# rm *.html~ *.htm~
if [ $# -lt 1 ]; then
printf '%s\n' "$0: usage: in-place commands [file ...]" >&2
exit 1
fi
commands="$1"
shift
for file; do
cp -fp -- "$file" "$file~"
sh -c "$commands" < "$file~" > "$file"
done