Unix tail and grep equivalent for Windows

前端 未结 2 1178
青春惊慌失措
青春惊慌失措 2021-01-26 22:16

We have the following unix command:

/usr/bin/tail -n 1 %{path} | grep --silent -F \"%{message}\" && rm -f %{path}%

This:

相关标签:
2条回答
  • 2021-01-26 23:00

    PowerShell solution:

    $path    = 'C:\path\to\your.txt'
    $message = 'message'
    
    Get-Item $path | ? { (Get-Content $_ -Tail 1).Contains($message) } | Remove-Item -Force
    

    If you want to run it from a command line, call it like this:

    powershell -Command "& {Get-Item $args[0] | ? { (Get-Content $_ -Tail 1).Contains($args[1]) } | Remove-Item -Force}" "'C:\path\to\your.txt'" "'message'"
    
    0 讨论(0)
  • 2021-01-26 23:06

    You can use tailhead.bat (pure batch script utility) that can be used to show lasts/fists lines of a file.Instead of Grep you can use findstr or find :

    tailhead.bat tailhead -file=%pathToFile% -begin=-3|find  "%message%"
    
    0 讨论(0)
提交回复
热议问题