{"id":340,"date":"2018-04-16T15:42:45","date_gmt":"2018-04-16T14:42:45","guid":{"rendered":"http:\/\/hslomka.de\/techno\/?p=340"},"modified":"2018-04-16T15:42:45","modified_gmt":"2018-04-16T14:42:45","slug":"mehrfache-eintrage-in-grosen-konfigurationsdateien-oder-protokollen-finden-windows-powershell-losung","status":"publish","type":"post","link":"https:\/\/techno.slomka.biz\/?p=340","title":{"rendered":"Mehrfache Eintr\u00e4ge in gro\u00dfen Konfigurationsdateien oder Protokollen finden (Windows Powershell L\u00f6sung)"},"content":{"rendered":"<p>Im Post <a href=\"http:\/\/hslomka.de\/techno\/?p=311\">Mehrfache Eintr\u00e4ge in gro\u00dfen Konfigurationsdateien oder Protokollen finden(Linux\/Unix)<\/a> habe ich gezeigt, wie mehrfach vorkommende Zeilen in Textdateien gefunden und automatisch entfernt werden k\u00f6nnen.<br \/>\nWindows hat im Zuge der Powershell ein m\u00e4chtiges Werkzeug, dass dies auch kann. (Seit dem Windows 10 1709 Update kann man nat\u00fcrlich auch einfach die eingebaute bash nehmen&#8230;.)<\/p>\n<p>Zur Demonstration verwende ich eine Datei <em>duplicates.txt<\/em>:<br \/>\n<code><\/code><\/p>\n<p class=\"kasten\">Get-Content -Path duplicates.txt<\/p>\n<p class=\"einzug\">Zeile kommt nur einmal vor<br \/>\nZeile kommt zweimal vor<br \/>\nZeile kommt dreimal vor<br \/>\nZeile kommt zweimal vor<br \/>\nZeile kommt dreimal vor<br \/>\nZeile kommt auch nur einmal vor<br \/>\nZeile kommt dreimal vor<\/p>\n<\/p>\n<p>Mehrfache vorkommende identische Zeilen und deren Anzahl findet und z\u00e4hlt man wie mit <code>Group-Object<\/code><br \/>\n<code><\/code><\/p>\n<p class=\"kasten\">Get-Content -Path duplicates.txt |Group-Object<\/p>\n<p class=\"einzug\">Count Name                      Group<br \/>\n&#8212;&#8211; &#8212;-                      &#8212;&#8211;<br \/>\n    1 Zeile kommt nur einmal&#8230; {Zeile kommt nur einmal vor}<br \/>\n    2 Zeile kommt zweimal vor   {Zeile kommt zweimal vor, Zeile kommt zweimal vor}<br \/>\n    3 Zeile kommt dreimal vor   {Zeile kommt dreimal vor, Zeile kommt dreimal vor, Zeile kommt dreimal vor}<br \/>\n    1 Zeile kommt auch nur e&#8230; {Zeile kommt auch nur einmal vor}<\/p>\n<\/p>\n<p>Wenn die Reihenfolge der Zeilen keine Bedeutung hat, ist es einfach, doppelte oder mehrfache Zeilen zu entfernen:<br \/>\n<code><\/code><\/p>\n<p class=\"kasten\">Get-Content -Path duplicates.txt | Sort-object -Unique<\/p>\n<p class=\"einzug\">Zeile kommt auch nur einmal vor<br \/>\nZeile kommt dreimal vor<br \/>\nZeile kommt nur einmal vor<br \/>\nZeile kommt zweimal vor<\/p>\n<p> oder<br \/>\n<code><\/code><\/p>\n<p class=\"kasten\">Get-Content -Path duplicates.txt | sort-object | Get-Unique<\/p>\n<p class=\"einzug\">Zeile kommt auch nur einmal vor<br \/>\nZeile kommt dreimal vor<br \/>\nZeile kommt nur einmal vor<br \/>\nZeile kommt zweimal vor<\/p>\n<\/p>\n<p>Die Ausgabe aller mehrfach vorkommenden Zeilen kann mit einer einfachen Abfrage geschehen:<br \/>\n<code><\/code><\/p>\n<p class=\"kasten\">Get-Content -Path duplicates.txt | Group | ? { $_.Count -gt 1 } |Select -ExpandProperty Name<\/p>\n<\/p>\n<p>Schwieriger wird es, die doppelten Zeilen zu entfernen, wenn die Reihenfolge in der Datei erhalten bleiben soll. Eine Hash Tabelle hilft an dieser Stelle weiter. Die zugrunde liegende Idee ist, f\u00fcr jede Zeile beim ersten Auftreten einen Eintrag in einer Hashtabelle vorzunehmen. Sollte dieser Zeile erneut auftreten, wird sie \u00fcbersprungen. Das ist der Code:<br \/>\n<code><\/code><\/p>\n<p class=\"kasten\">$hash = $null<br \/>\n$hashTable = @{}<br \/>\nGet-Content .\\duplicates.txt | ForEach-Object {<br \/>\n&nbsp;&nbsp;if ($hashTable.Item($_) -eq $null) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$_<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$hashTable.Add($_,&#8221;found&#8221;)<br \/>\n&nbsp;&nbsp;}<br \/>\n}<\/p>\n<p class=\"einzug\">Zeile kommt nur einmal vor<br \/>\nZeile kommt zweimal vor<br \/>\nZeile kommt dreimal vor<br \/>\nZeile kommt auch nur einmal vor<\/p>\n<\/p>\n<p>Die ersten beiden Zeilen initialisieren die Tabelle <em>$hashTable<\/em>. Dann wird f\u00fcr jede neue Zeile ein Eintrag mit dem Wert &#8220;found&#8221; hinzugef\u00fcgt (eine 1, ein true oder \u00e4hnlich w\u00fcrden es auch tun, Hauptsache ein Eintrag f\u00fcr die Zeile existiert.).<\/p>\n<p><strong>Achtung:<\/strong> Die Ausgabe von $hashTable.Keys gibt die Reihenfolge nicht wie in der Datei aus:<br \/>\n<code><\/code><\/p>\n<p class=\"kasten\">$hashTable.Keys\n<\/p>\n<p class=\"einzug\">Zeile kommt dreimal vor<br \/>\nZeile kommt zweimal vor<br \/>\nZeile kommt nur einmal vor<br \/>\nZeile kommt auch nur einmal vor<\/p>\n<\/p>\n<p>Sehr hilfreich sind die <a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/15\/easily-create-a-powershell-hash-table\/\" rel=\"noopener\" target=\"_blank\">Erkl\u00e4rungen zu Powershell Hash Tabellen<\/a> von den <a href=\"https:\/\/social.technet.microsoft.com\/profile\/The+Scripting+Guys\" rel=\"noopener\" target=\"_blank\">Scripting Guys<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Im Post Mehrfache Eintr\u00e4ge in gro\u00dfen Konfigurationsdateien oder Protokollen finden(Linux\/Unix) habe ich gezeigt, wie mehrfach vorkommende Zeilen in Textdateien gefunden und automatisch entfernt werden k\u00f6nnen. Windows hat im Zuge der Powershell ein m\u00e4chtiges Werkzeug, dass dies auch kann. (Seit dem Windows 10 1709 Update kann man nat\u00fcrlich auch einfach die eingebaute bash nehmen&#8230;.) Zur Demonstration verwende ich eine Datei duplicates.txt: Get-Content -Path duplicates.txt Zeile kommt nur einmal vor Zeile kommt zweimal vor Zeile kommt dreimal vor Zeile kommt zweimal vor Zeile kommt dreimal vor Zeile kommt auch nur einmal vor Zeile kommt dreimal vor Mehrfache vorkommende identische Zeilen und deren Anzahl findet und z\u00e4hlt man wie mit Group-Object Get-Content -Path duplicates.txt |Group-Object Count Name Group &#8212;&#8211; &#8212;- &#8212;&#8211; 1 Zeile kommt nur einmal&#8230; {Zeile kommt nur einmal vor} 2 Zeile kommt zweimal vor {Zeile kommt zweimal vor, Zeile kommt zweimal vor} 3 Zeile kommt dreimal vor {Zeile kommt dreimal vor, Zeile kommt dreimal vor, Zeile kommt dreimal vor} 1 Zeile kommt auch nur e&#8230; {Zeile kommt auch nur einmal vor} Wenn die Reihenfolge der Zeilen keine Bedeutung hat, ist es einfach, doppelte oder mehrfache Zeilen zu entfernen: Get-Content -Path duplicates.txt | Sort-object -Unique Zeile kommt auch nur einmal vor Zeile kommt dreimal vor Zeile kommt nur einmal vor Zeile kommt zweimal vor oder Get-Content -Path duplicates.txt | sort-object | Get-Unique Zeile kommt auch nur einmal vor Zeile kommt dreimal vor Zeile kommt nur einmal vor Zeile kommt zweimal vor Die Ausgabe aller mehrfach vorkommenden Zeilen kann mit einer einfachen Abfrage geschehen: Get-Content -Path duplicates.txt | Group | ? { $_.Count -gt 1 } |Select -ExpandProperty Name Schwieriger wird es, die doppelten Zeilen zu entfernen, wenn die Reihenfolge in der Datei erhalten bleiben soll. Eine Hash Tabelle hilft an dieser Stelle weiter. Die zugrunde liegende Idee ist, f\u00fcr jede Zeile beim ersten Auftreten einen Eintrag in einer Hashtabelle vorzunehmen. Sollte dieser Zeile erneut auftreten, wird sie \u00fcbersprungen. Das ist der Code: $hash = $null $hashTable = @{} Get-Content .\\duplicates.txt | ForEach-Object { &nbsp;&nbsp;if ($hashTable.Item($_) -eq $null) { &nbsp;&nbsp;&nbsp;&nbsp;$_ &nbsp;&nbsp;&nbsp;&nbsp;$hashTable.Add($_,&#8221;found&#8221;) &nbsp;&nbsp;} } Zeile kommt nur einmal vor Zeile kommt zweimal vor Zeile kommt dreimal vor Zeile kommt auch nur einmal vor Die ersten beiden Zeilen initialisieren die Tabelle $hashTable. Dann wird f\u00fcr jede neue Zeile ein Eintrag mit dem Wert &#8220;found&#8221; hinzugef\u00fcgt (eine 1, ein true oder \u00e4hnlich w\u00fcrden es auch tun, Hauptsache ein Eintrag f\u00fcr die Zeile existiert.). Achtung: Die Ausgabe von $hashTable.Keys gibt die Reihenfolge nicht wie in der Datei aus: $hashTable.Keys Zeile kommt dreimal vor Zeile kommt zweimal vor Zeile kommt nur einmal vor Zeile kommt auch nur einmal vor Sehr hilfreich sind die Erkl\u00e4rungen zu Powershell Hash Tabellen von den Scripting Guys.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26],"tags":[65,78,88,108],"class_list":["post-340","post","type-post","status-publish","format-standard","hentry","category-powershell","category-windows","tag-hashtabellen","tag-powershell","tag-shell","tag-windows"],"_links":{"self":[{"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/posts\/340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/comments?post=340"}],"version-history":[{"count":0,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"wp:attachment":[{"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}