2016-08-25 2 views
0

Ich habe die folgende Zeile, die mir eine Liste von Dateien gibt.Wie wird nach einer Zeichenfolge innerhalb des Inhalts jeder Datei in einer Liste gesucht? -Powershell

{Get-ChildItem -Recurse -Force $filePath | sort-object -property CreationTime -descending | where { $_.CreationTime.dayofyear -eq $today } | Where-Object { ($_.PSIsContainer -eq $false) -and ($_.Name -like "*FILE1*" -or $_.Name -like "*FILE2*" -or $_.Name -like "*FILE3*") } | Select-Object Name,CreationTime,CreationDay | Format-Table -AutoSize * } 

Hier ist meine Ergebnisse:

Namen Creation

  • FILE1.022291 2016.08.25 08.25.07
  • FILE2.022285 2016.08.25 2:25:10 AM
  • FILE3.022281 8/25/2016 2:25:08 AM

Ich möchte nach zwei bestimmten Strings innerhalb jeder Datei der Ergebnisse suchen, und wenn eine der spezifischen Strings gefunden wird, zeigen Sie die gesamte Zeile, die die Zeichenfolge unter jedem Ergebnis ist.

Hier ist, was ich hoffe, dass es wie

Namen Creation

  • FILE1.022291 2016.08.25 08.25.07
    • THIS_IS_ALL_OF_LINE_4_WITH_SPECIFICSTRING1
    • aussehen kann
  • FILE2.022285 8/25/2016 2:25:10 AM
    • THIS_IS_ALL_OF_LINE_16_WITH_SPECIFICSTRING2
  • FILE3.022281 2016.08.25 02.25.08
    • THIS_IS_ALL_OF_LINE_7_WITH_SPECIFICSTRING1

Jede Hilfe wäre sehr dankbar. Ich bin etwas neu in Powershell, also werde ich mein Bestes tun, um bei Fragen zu helfen.

+0

Welche Version von Powershell Sie (Check '$ PSVersionTable') –

+0

PSVersion 5.1.14393.0 –

Antwort

1

Sie können Select-String für diese Information verwenden. Holen Sie sich den Dateifilter für das gesuchte Datum und verwenden Sie Select-String. Select-String ist Dateiname: Zeilennummer: Match.

PS> gci -File -Path $filePath file* | ? creationtime -gt (get-date).AddMinutes(-30) | Select-String -Pattern "SpecificString1|SpecificString2" 

FILE1.022291:2:SpecificString1 
FILE2.022285:4:SpecificString2 
FILE3.022281:1:SpecificString1 

Und hier ist eine Formatierung mit benannten Ausdrücken in der Select-Anweisung.

PS> gci -File -Path $filePath file* | ? creationtime -gt (get-date).AddMinutes(-30) | Select @{n='StringFound';e={$_ | Select-String -Pattern "SpecificString1|SpecificString2"}}, CreationTime; 

StringFound       CreationTime 
-----------       ------------ 
C:\temp\FILE1.022291:2:SpecificString1 8/25/2016 1:09:50 PM 
C:\temp\FILE2.022285:4:SpecificString2 8/25/2016 1:09:50 PM 
C:\temp\FILE3.022281:1:SpecificString1 8/25/2016 1:09:50 PM 

Hier arbeitet mit dem select-string Ausgabe:

PS> gci -File -Path $filePath file* | ? creationtime -gt (get-date).AddMinutes(-600) | Select-String -Pattern "Speci 
ficString1|SpecificString2" -AllMatches | select LineNumber, FileName, Line, matches 

LineNumber Filename  Line      Matches 
---------- --------  ----      ------- 
     2 FILE1.022291 SpecificString1   {SpecificString1} 
     4 FILE1.022291 sfdgdsgsdf SpecificString2 {SpecificString2} 
     6 FILE1.022291 SpecificString2   {SpecificString2} 
     4 FILE2.022285 SpecificString2   {SpecificString2} 
     1 FILE3.022281 SpecificString1   {SpecificString1} 

Bespannungs zwei Auswahl Zeichenfolge für eine einfache negieren.

PS> gc C:\tmp\test.txt 
this is foo 
this is foo*bag 
this is the bang 

PS> gc C:\tmp\test.txt | Select-String -Pattern 'foo' -AllMatches | Select-String -SimpleMatch '*' -NotMatch -AllMatches | fl 


IgnoreCase : True 
LineNumber : 1 
Line  : this is foo 
Filename : InputStream 
Path  : InputStream 
Pattern : foo 
Context : 
Matches : {foo} 
+0

irThemis verwenden, dies gearbeitet, aber ich fügte hinzu: "-allmatches", um den Select-String.Außerdem trifft die Auswahlzeichenfolge mehrere Male innerhalb weniger Dateien, und die Ausgabe sieht folgendermaßen aus: {C: \ temp \ FILE1.022310: 1: STRINGMATCHED1, C: \ temp \ FILE1.022310: 213: STRINGMATCHED1, C: \ temp \ FILE1.022310: 221: STRINGMATCHED1, C: \ tem ... C: \ temp \ FILE2.022311: 1: STRINGMATCHED2 ... Kann ich den Pfad unterdrücken und nur die Dateinamen anzeigen? Kann ich die Ergebnisse auch als Liste anzeigen? –

+0

Ja Select-String -allmatches, tritt ein Objekt zurück, ich werde den Beitrag mit der Aussage aktualisieren. – irThemis

+0

Mike P, Wenn dies half, bitte markieren Sie es als Antwort. – irThemis

Verwandte Themen