Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在文本文件中搜索特定单词(如果找到),将整行复制到powershell中的新文件_Powershell - Fatal编程技术网

在文本文件中搜索特定单词(如果找到),将整行复制到powershell中的新文件

在文本文件中搜索特定单词(如果找到),将整行复制到powershell中的新文件,powershell,Powershell,我想在文本文件中搜索字符串失败,如果找到,将整行复制到另一个文件中 test.txt的争用: : \Hardware\Disk Enclosure 27 diskslot1 failed : \Hardware\Disk Enclosure 27 diskslot2 normal : \Hardware\Disk Enclosure 27 diskslot3 normal : \Hardware\Disk E

我想在文本文件中搜索字符串
失败
,如果找到,将整行复制到另一个文件中

test.txt的争用:

: \Hardware\Disk Enclosure 27 diskslot1 failed : \Hardware\Disk Enclosure 27 diskslot2 normal : \Hardware\Disk Enclosure 27 diskslot3 normal : \Hardware\Disk Enclosure 27 diskslot4 normal :\Hardware\Disk Enclosure 27 diskslot1失败 :\Hardware\Disk Enclosure 27 diskslot2正常 :\Hardware\Disk Enclosure 27 diskslot3正常 :\Hardware\Disk Enclosure 27 diskslot4正常 请在脚本上提供帮助

尝试以下操作:

select-string -path "D:\Textfile.txt" -Pattern "Text" | select line | out-file d:\outputfile.txt -append
编辑不带行的输出

$Output = select-string -path "d:\textfile.txt" -pattern "Text". 
$Output.line | out-file d:\outputfile.txt -append
试试这个:

select-string -path "D:\Textfile.txt" -Pattern "Text" | select line | out-file d:\outputfile.txt -append
编辑不带行的输出

$Output = select-string -path "d:\textfile.txt" -pattern "Text". 
$Output.line | out-file d:\outputfile.txt -append

此脚本打印包含“myword”的每一行:


此脚本打印包含“myword”的每一行:


Select-String
cmdlet用于执行以下操作。运行
Get Help Select String
,您将看到以下内容:“默认情况下,Select String在每一行中查找第一个匹配项,对于每个匹配项,它将显示文件名、行号以及包含匹配项的行中的所有文本。”
Select String
cmdlet用于执行类似操作。运行
Get Help Select String
,您将看到以下内容:“默认情况下,Select String会在每一行中找到第一个匹配项,对于每个匹配项,它会显示文件名、行号以及包含匹配项的行中的所有文本。”它的工作方式与我预期的一样。。但文件中有条目“行”。是否可以在没有单词“Line”的情况下追加到文件中?没有问题。如果您满意,请将该线程标记为solvedIts,并按照我的预期工作。。但文件中有条目“行”。是否可以在没有单词“Line”的情况下追加到文件中?没有问题。如果您满意,请将线程标记为已解决