在使用Powershell在文本文件中找到特定行之后,如何停止Get-Content -wait?按需退出管道 [英] How to stop Get-Content -wait after I find a particular line in the text file using powershell? Exit a pipeline on demand

查看:306
本文介绍了在使用Powershell在文本文件中找到特定行之后,如何停止Get-Content -wait?按需退出管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Powershell(版本为5.1)中使用以下脚本:

I am using this below script in Powershell (Version is 5.1):

Get-Content -Path path\to\text\file\to\be\read.txt -Wait

现在,即使文件没有更新,此内容仍可继续读取. 在文本文件中找到特定行后如何停止? 还有其他方法可以阻止这种情况吗?

Now this continues to read even after the file is getting no update. How can I stop after I find particular line in the text file? Is there any other way to stop this on condition?

推荐答案

您可以通过管道传输输出并使用foreach来检查每行,如果该行等于某个字符串,则可以使用break停止命令:

You can pipe the output and use foreach to check each line, if the line equals a certain string you can use break to stop the command:

Get-Content path\to\text\file\to\be\read.txt -wait | % {$_ ; if($_ -eq "yourkeyword") {break}}

例如,如果您运行上面的命令并在另一个shell中执行以下操作:

For example, if you run the command above and do the following in another shell:

"yourkeyword" | Out-File path\to\text\file\to\be\read.txt -Append

Get-Content显示新行并停止.

说明:

| %-foreach行中的文件行或函数运行时添加到文件中的行

| % - foreach line that is in the file or gets added to the file while the function runs

$_;-首先写出foreach的行,然后执行另一条命令

$_; - first write out the line of the foreach then do another command

if($_ -eq "yourkeyword") {break}-如果foreach的行与所需的关键字/行相等,请停止Get-Content

if($_ -eq "yourkeyword") {break} - if the line of the foreach is equal to the keyword/line you want, stop the Get-Content

这篇关于在使用Powershell在文本文件中找到特定行之后,如何停止Get-Content -wait?按需退出管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆