【问题标题】:How to replace a random IP-address in a file (Perl, Windows)如何替换文件中的随机 IP 地址(Perl、Windows)
【发布时间】:2019-12-27 18:38:09
【问题描述】:

如何使用 Windows Perl 替换 TXT 文件中的随机(未知)IP 地址? (不幸的是,我只找到了主要针对 Linux/sed 的解决方案)

我的文本文件包含 FTP 命令,只包含一个 IP 地址:

open ftp://user:password@123.123.123.123
cd c:/temp
mkdir newdir

等等

我试过了,但没有任何改变。

perl -pe s/[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}/10.10.111.222/g FtpCommands.txt > FtpCommands_mod.txt

谢谢!

【问题讨论】:

标签: windows file perl replace ip-address


【解决方案1】:

如果您的文件具有 IPv4 地址,则使用 Regexp::Common 模块可以轻松实现这一点:

perl -MRegexp::Common=net -pe "s/$RE{net}{IPv4}/10.10.111.222/" FtpCommands.txt > FtpCommands_mod.txt

【讨论】:

  • 在 Windows 命令行中使用双引号 ... "s/$RE{net}{IPv4}/10.10.111.222/"
  • 谢谢你们俩!它可以在 Windows 上使用 Regexp 模块,也可以在没有它的情况下使用:perl -MRegexp::Common=net -pe "s/$RE{net}{IPv4}/10.10.111.222/" FtpCommands.txt > FtpCommands_mod.txt
    perl -pe "s/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/10.10.111.222/" FtpCommands.txt > FtpCommands_mod.txt
  • @mob 完成。不过看起来很有趣。我假设标准 Windows shell 不使用 $ 进行变量替换吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 2017-08-13
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多