我们有以下 unix 命令:
/usr/bin/tail -n 1 %{path} | grep --silent -F "%{message}" && rm -f %{path}%
这个:
/usr/bin/tail -n 1 %{path}
获取path
变量引用的文件中的最后一行| grep --silent -F "%{message}"
将输出通过管道传输到另一个命令grep
,该命令检查前一个命令的输出是否等于的值消息
&& rm -f %{path}%
如果值相等,则删除path
引用的文件
上面的行位于配置文件中,允许调用底层操作系统。
我想在 Windows 上复制该功能。 我试过这个:
command => 'powershell -Command "& {Get-Item $args[0] | ? { (Get-Content $_ -Tail 1).Contains($args[1]) }| Remove-Item -Force}" "'%path%'" "'%message%'"'
抛出此错误:
Error: Expected one of #, {, } at line 15, column 131 (byte 498)
第 15 行是配置文件中包含上述内容的行。
谢谢
最佳答案
PowerShell 解决方案:
$path = 'C:\path\to\your.txt'
$message = 'message'
Get-Item $path | ? { (Get-Content $_ -Tail 1).Contains($message) } | Remove-Item -Force
如果您想从命令行运行它,请像这样调用它:
powershell -Command "& {Get-Item $args[0] | ? { (Get-Content $_ -Tail 1).Contains($args[1]) } | Remove-Item -Force}" "'C:\path\to\your.txt'" "'message'"
关于powershell - Windows 下的 Unix tail 和 grep 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31136733/