gpt4 book ai didi

Perl 脚本中的 Perl 内联替换

转载 作者:行者123 更新时间:2023-12-01 13:03:24 24 4
gpt4 key购买 nike

我正在尝试编写一个带有内联替换的 perl 脚本。我找到了这个脚本示例来执行此操作,但不确定如何使用它,因为我不确定如何指定文件和处理程序?

$^I = '.bak'  # Call for in-place editing; make backups with a .bak suffix

while (<>)
{
s/foo/bar/
print;
}

最佳答案

<>是一个特殊的文件句柄。

The special file handle: <>

Like the default variable, the special file handle -- written as <> is a short cut in the language added to make programs easier to write. The special file handle treats all command line arguments as file names, and opens each in turn. If there are no command line arguments then it opens and reads from STDIN. As per the UNIX convention, if "-" is given as a command line argument it opens STDIN in place of a file.

Source

设置$^I变量启用就地编辑 - 换句话说,文件被直接修改。

要使用此脚本,您可以执行以下操作:

perl yourscript.pl somefile.txt

关于Perl 脚本中的 Perl 内联替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4480401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com