简体   繁体   English

Tcl相当于一个Perl单线程

[英]Tcl equivalent to a Perl one-liner

I found myself using a lot of Perl one-liner regexes in my code when I need some small processing on ASCII files which I don't want to open for editing. 当我需要对ASCII文件进行一些小处理时,我发现自己在代码中使用了很多Perl单行正则表达式,我不想打开它进行编辑。

Is there a Tcl equivalent to this Perl one-liner command? 是否有一个Tcl相当于这个Perl单线命令?

perl -i.bak -pe 's/old/new/gi' filename

look at the owh script of Richard Suchenwirth http://wiki.tcl.tk/906 which gives this sort of functionality. 看看Richard Suchenwirth的博客脚本http://wiki.tcl.tk/906 ,它提供了这种功能。 Joachim 约阿希姆

I once wrote a Tcl script to provide Perl-like command line options. 我曾经写过一个Tcl脚本来提供类似Perl的命令行选项。 I never used it much myself -- it ended being an exercise in working with sending code to an Tcl interp. 我自己从未使用过它 - 它最终成为了将代码发送到Tcl interp的练习。 If you're interested, the code is at https://bitbucket.org/glenn_j/tcl-command-line-one-liners/src 如果您有兴趣,请访问https://bitbucket.org/glenn_j/tcl-command-line-one-liners/src

I use this script to execute shell pipelines on files in-place, but you could change commands="$1" to printf '%s' "$1" > sometemporaryfile and sh -c "$commands" to tclsh sometemporaryfile . 我使用这个脚本就地对文件执行shell管道 ,但你可以将commands="$1"改为printf '%s' "$1" > sometemporaryfilesh -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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM