我有两组操作,在第一组中我查找包含字符串的文件,然后在第二组中我使用该列表提取包含另一个字符串的行然后编辑它们。
$List_Of_Files = Get-ChildItem "$outputfolder*.html" -recurse |
Select-String -pattern "https://www.youtube.com" | group path |
select name -ExpandProperty Name
$List_Of_Titles = @(Get-Content $List_Of_Files | Where-Object { $_.Contains("<title>") }) |
Foreach-Object {
$content = $_ -replace " <title>", " <video:title>";
$content -replace "</title>", "</video:title>"
}
代码按预期工作,但问题是我需要第一组操作将结果输出到文本文件中,然后在第二组中使用该文件,该文件也应将结果输出到另一个文本文件中。
我尝试过以下操作,但第二套不会创建文件,但也不会给我任何错误。
Get-ChildItem "$outputfolder*.html" -recurse |
Select-String -pattern "https://www.youtube.com" | group path |
select name -ExpandProperty Name | Set-Content "c:\List_Of_Files.txt"
@(Get-Content "c:\List_Of_Files.txt" | Where-Object { $_.Contains("<title>") }) |
Foreach-Object {
$content = $_ -replace " <title>", " <video:title>";
$content -replace "</title>", "</video:title>"
} | Set-Content "c:\list_of_titles.txt"
我试图以不同的方式修改它,但无法弄清楚如何使它工作。
答案 0 :(得分:1)
c:\List_Of_Files.txt
包含文件路径列表,您尝试按路径是否包含"<title>"
过滤该列表,结果如何没有比赛。
(我没有解释为什么你的第一个片段有效。)
您的问题源于对通过管道传递的对象的混淆:您从文件路径(字符串)开始,然后威胁它们就好像它们是文件一样'内容。
相反,我假设您打算测试由其路径标识的每个文件的内容。
快速解决方法是:
Get-Content "c:\List_Of_Files.txt" | Where-Object { Select-String -Quiet '<title>' $_ }
但请注意,您还必须相应地调整ForEach-Object
命令:
Foreach-Object {
# Read the content of the file whose path was given in $_,
# and modify it.
# (If you don't want to save the modifications, omit the `Set-Content` call.)
$content = ((Get-Content $_) -replace " <title>", " <video:title>");
$content = $content -replace "</title>", "</video:title>";
# Save modifications back to the input file (if desired).
Set-Content -Value $content -Path $_;
# $content is the entire document, so to output only the title line(s)
# we need to match again:
$content -match '<video:title>'
# Note: This relies on the title HTML element to be on a *single* line
# *of its own*, which may not be the case;
# if it isn't, you must use proper HTML parsing to extract it.
}
把它们放在一起:
Get-Content "c:\List_Of_Files.txt" | Where-Object { Select-String -Quiet '<title>' $_ } |
Foreach-Object {
$content = ((Get-Content $_) -replace " <title>", " <video:title>");
$content = $content -replace "</title>", "</video:title>";
Set-Content -Value $content -Path $_;
$content -match '<video:title>'
} | Set-Content "c:\list_of_titles.txt"
请注意,通过删除使用Select-String
并在ForEach-Object
块内执行过滤的过滤步骤,您可以提高整个命令的效率。
此外,可以优化字符串替换,或者最好使用真正的HTML解析来处理。