【问题标题】:Tcl equivalent to a Perl one-linerTcl 等价于 Perl 单行
【发布时间】:2012-11-02 18:31:45
【问题描述】:

当我需要对不想打开进行编辑的 ASCII 文件进行一些小处理时,我发现自己在代码中使用了很多 Perl 单行正则表达式。

是否有与这个 Perl 单行命令等效的 Tcl?

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

【问题讨论】:

  • 大家好。首先感谢您的回答。此外,我继续自己的调查,目前 Tcl 似乎没有问题中描述的 Perl 单行等效项。

标签: regex tcl


【解决方案1】:

查看 Richard Suchenwirth http://wiki.tcl.tk/906 的 owh 脚本,它提供了这种功能。 约阿希姆

【讨论】:

    【解决方案2】:

    我曾经写过一个 Tcl 脚本来提供类似 Perl 的命令行选项。我自己从来没有经常使用它——它最终成为了一个将代码发送到 Tcl 插值器的练习。如果你有兴趣,代码在https://bitbucket.org/glenn_j/tcl-command-line-one-liners/src

    【讨论】:

      【解决方案3】:

      我使用此脚本在文件上就地执行 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        • 2012-05-23
        • 2012-02-21
        • 2023-04-10
        • 1970-01-01
        相关资源
        最近更新 更多