必须从文件末尾读取文件abc.txt
,直到读取名为"Install location: "C:\Users\desktop\1234_5467.dat"
的特定字符串为止。
这里的问题是我们重复出现了像“ 1234_5467.dat”这样的字符串,我希望内容从头开始读取到“安装位置:“ C:\ Users \ desktop \ 1234_5467.dat”“,而无需使用尾因为我们在文件中没有指定字符串行的明确行号。
我正在使用从文件末尾读取内容,因为每次都会附加文件中的内容,因此最好从文件末尾开始。
我尝试使用Tail,尽管它可以正常工作,但由于字符串行数不可预测,因此无法继续操作
05/28/2019 18:25:08 - Successfully deleted HKEY_LOCAL_MACHINE\SOFTWARE\EMC\Symmwin\SystemInstallState Registry Key
05/28/2019 18:35:43 - =========================================================================
05/28/2019 18:35:43 - Installation Started
05/28/2019 18:35:43 - Installation Location: c:\Temp\456.exe
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:20 - SnippT completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:37:43 - --------------------------------------------------
05/28/2019 18:37:43 - This is the point of no return!
05/28/2019 18:37:43 - --------------------------------------------------
05/28/2019 18:37:44 - Installing Windows Features
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:48:21 - --------------------------------------------------
05/28/2019 18:48:21 - Install Complete.
05/28/2019 18:48:21 - --------------------------------------------------
05/28/2019 18:35:43 - =========================================================================
5/28/2019 18:35:43 - Installation Started
05/28/2019 18:35:43 - Installation Location: c:\Temp\3456.exe
05/28/2019 18:35:43 - Installation Started
05/28/2019 18:35:43 - Installation Location: c:\Temp\456.exe
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:20 - SnippT completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:25:03 - Aborting the installation.
Aborting the installation!.
C:\ drive does not have enough space to install the application.
The minimum space required to install Enginuity installer in C:\ Drive is 2 GB.
05/28/2019 18:35:43 - =========================================================================
05/28/2019 18:35:43 - Installation Started
05/28/2019 18:35:43 - Installation Location: c:\Temp\3456.exe
05/28/2019 18:37:44 - Installing Windows Features
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:37:43 - This is the point of no return!
05/28/2019 18:37:43 - --------------------------------------------------
05/28/2019 18:37:44 - Installing Windows Features
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:48:21 - --------------------------------------------------
05/28/2019 18:48:21 - Install Complete.
05/28/2019 18:48:21 - --------------------------------------------------
我需要从上次出现c:\ Temp \ 3456.exe到结束的文件内容
05/28/2019 18:35:43 - Installation Location: c:\Temp\3456.exe
05/28/2019 18:37:44 - Installing Windows Features
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:37:43 - This is the point of no return!
05/28/2019 18:37:43 - --------------------------------------------------
05/28/2019 18:37:44 - Installing Windows Features
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:48:21 - --------------------------------------------------
05/28/2019 18:48:21 - Install Complete.
05/28/2019 18:48:21 - --------------------------------------------------
答案 0 :(得分:0)
这个新的文本示例使事情与您之前发布的有所不同。 使用这个新示例,以下方法应该起作用:
$file = 'D:\blah.txt'
$search = '*Installation Location: C:\Temp\*.exe*'
# get the data as string array, remove empty or whitespace only elements
$data = Get-Content -Path $file | Where-Object { ![string]::IsNullOrWhiteSpace($_) }
if (($data -like $search).Count -eq 0) {
Write-Warning "Search string '$search' not found in file '$file'"
}
else {
# read the array from last to first line and collect the lines in $result
$result = @()
for ($i = ($data.Count -1); $i -ge 0; $i--) {
$result += $data[$i]
if ($data[$i] -like $search) {
# keep adding lines to the result until the divider string
# '=============================================' is found
while (--$i -ge 0 -and $data[$i] -notmatch '.*=+') {
$result += $data[$i]
}
# also add the '=============================================' ?
# $result += $data[$i]
# exit the for loop because we're done
break
}
}
# reverse the collected lines
[array]::Reverse($result)
$result
}
结果:
05/28/2019 18:35:43 - Installation Started 05/28/2019 18:35:43 - Installation Location: c:\Temp\3456.exe 05/28/2019 18:37:44 - Installing Windows Features 05/28/2019 18:42:20 - Tools Installation Completed 05/28/2019 18:42:31 - Adobe Installation Completed. 05/28/2019 18:42:31 - Installing Paint 05/28/2019 18:48:00 - MS OFFICE Completed 05/28/2019 18:48:06 - Font Completed 05/28/2019 18:48:06 - Installing Windows Master File 05/28/2019 18:48:06 - Notepad++ Completed 05/28/2019 18:48:06 - Notepad Settings 05/28/2019 18:48:15 - ABC Installation Completed 05/28/2019 18:37:43 - This is the point of no return! 05/28/2019 18:37:43 - -------------------------------------------------- 05/28/2019 18:37:44 - Installing Windows Features 05/28/2019 18:42:20 - Tools Installation Completed 05/28/2019 18:42:31 - Adobe Installation Completed. 05/28/2019 18:42:31 - Installing Paint 05/28/2019 18:48:00 - MS OFFICE Completed 05/28/2019 18:48:06 - Font Completed 05/28/2019 18:48:06 - Installing Windows Master File 05/28/2019 18:48:06 - Notepad++ Completed 05/28/2019 18:48:06 - Notepad Settings 05/28/2019 18:48:15 - ABC Installation Completed 05/28/2019 18:48:21 - -------------------------------------------------- 05/28/2019 18:48:21 - Install Complete. 05/28/2019 18:48:21 - --------------------------------------------------
如您所见,我现在添加了一个测试,以检查您正在使用的搜索字符串是否可以在文件中找到。如果在文件中找不到该文件,则会看到警告消息:
WARNING: Search string '*Installation Location: C:\Temp\z*.exe*' not found in file 'D:\blah.txt'
,此后脚本便退出。这样,您就可以尝试创建一个在给定实际文件的情况下对您有用的搜索字符串。