2016-06-24 4 views
1

2つの操作セットがあります。1番目は文字列を含むファイルを検索し、2番目はそのリストを使用して別の文字列を入力して編集します。テキストファイルに保存されたファイルのリストを使用してpowershellで文字列を検索する

$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>" 
    } 

コードが期待通りに動作しますが、問題は、私はテキストファイルに出力結果を事業の第一セットを必要として、その別のテキストファイルにする必要があり、出力結果を第二セットでそのファイルを使用することです。

私は以下を試しましたが、2番目のセットはファイルを作成しませんが、私にもエラーはありません。

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

ファイルc:\ List_Of_Files.txtは作成されますか? – DAXaholic

+0

はいそれは作成されます –

+1

ちょうどタグを含むダミーファイルでそれをテストし、最後のパイプラインが機能しました。 List_Of_Files.txtのコンテンツのダイジェストとして表示できますか? – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1830293/">DAXaholic</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">答えて</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">1<i class="fa fa-thumbs-up"></i></span> <i class="fa fa-check fa-2x"></i> </div> <div class="post-offset"> <div class="answer fmt"> <p><code class="prettyprint-override">c:\List_Of_Files.txt</code>は<em>ファイルパス</em>のリストが含まれていて、あなたは<em>パス</em>はありません試合で結果<code class="prettyprint-override">"<title>"</code>を、含まれているかどうかによって、そのリストをフィルタ処理しようとしています。あなたは<em>ファイルパス</em>(文字列)で開始し、脅威それらは:<br> (私はあなたの第一スニペットは働いていた理由のために何の説明もありません。)</p> <p>あなたの問題は、パイプラインを通過されているかのオブジェクトの上に<strong>混乱から茎あたかもそれらがファイル '<em>の内容</em>であるかのように。</strong></p> <strong> <p>代わりに、私はあなたの道で特定された各ファイルのの内容<em>をテストすることを意味すると仮定します。</em></p><em> <p>クイックフィックスは、次のようになります。</p> <pre><code class="prettyprint-override">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. } </code></pre> <p>をそれをすべて一緒に入れて:あなたもそれに応じて<code class="prettyprint-override">ForEach-Object</code>コマンドを適応しなければならないことが</p> <pre><code class="prettyprint-override">Get-Content "c:\List_Of_Files.txt" | Where-Object { Select-String -Quiet '<title>' $_ } </code></pre> <p>注意、</p> <pre><code class="prettyprint-override">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" </code></pre> <p>コマンド全体をより効率的にするには、 <code class="prettyprint-override">Select-String</code>を使用し、<code class="prettyprint-override">ForEach-Object</code>ブロック内でフィルタリングを実行します。</p> <p>また、文字列の置換は、最適化することができます。また、真のHTML解析で処理することもできます。</p></em></strong> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/38018694">出典</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2016-06-24 17:04:44</span> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/45375/">mklement0</a></span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">'ForEach-Object'ブロックの最後の行にコードを"仕上げ "する方法がわかりません。このようなテキストファイルにパスを入れました。' $ _ c:\ list_of_titles.txt'私はエラーが発生する - 式や文で予期しないトークン 'c:\ list_of_titles.txt'。それをどう修正するか教えてください。混乱のため申し訳ありませんが、私は明らかにPowerShellについてよく分かりません。 – <span class="text-secondary"> <small> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">@PeterPavelka:私のコードをワンライナーに変換して、 '$'の前に ';'を忘れてしまったように思えます(行末に '; 'は必要ありません) 。しかしもっと重要な質問です。「c:\ list_of_titles.txt」に何を保存しようとしていますか?それは_fileのパスか_実際のタイトルの文字列ですか?後者の場合、コードは機能しません。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/45375/">mklement0</a></span> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">いいえ、あなたが示唆したように最後に「c:\ list_of_titles.txt」を追加する以外は何もしませんでした。元の質問で説明したように実際のタイトルを保存する必要があります。 'https:// www.youtube.com'という文字列を含むファイルの場合、2番目のリストを使用して文字列' ' ''を抜き出して、すべての行にタイトルタグをビデオタイトルタグで置き換えます。結果をファイルに保存するので、実際のタイトルが出力として必要です。 –

関連する問題