{"id":501,"date":"2018-08-31T07:34:51","date_gmt":"2018-08-31T06:34:51","guid":{"rendered":"http:\/\/hslomka.de\/techno\/?p=501"},"modified":"2026-03-25T10:16:08","modified_gmt":"2026-03-25T10:16:08","slug":"powershell-tail-und-head-vereint","status":"publish","type":"post","link":"https:\/\/techno.slomka.biz\/?p=501","title":{"rendered":"\ud83d\udca1Powershell Tip: Tail und Head vereint"},"content":{"rendered":"\n<p><strong>Ausgabe der letzten und der ersten n-Zeilen in einemBefehl<\/strong><br><\/p>\n\n\n\n<p><em>In Unix\/Linux Derivaten sind die Befehle tail und Head oft benutzte Werkzeuge zur Ausgabe der ersten\/letzten Zeilen einer Datei oder der Ergebnisse eines Befehls. Direkt kombinieren kann man sie nicht. Hier sind sed\/awk und andere Werkzeuge und L\u00f6sungen gefordert. <\/em>Powershell bietet eine sehr effiziente L\u00f6sung.<\/p>\n\n\n\n<p>Der erste Ansatz w\u00e4re die Ausgabe mit Select -Last und Select -First zu behandeln.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers kasten\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">PowerShell<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>Get-ChildItem somefolder | Select -First 10<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #82AAFF\">Get-ChildItem<\/span><span style=\"color: #EEFFFF\"> somefolder <\/span><span style=\"color: #89DDFF\">|<\/span><span style=\"color: #EEFFFF\"> Select <\/span><span style=\"color: #89DDFF\">-<\/span><span style=\"color: #EEFFFF\">First <\/span><span style=\"color: #F78C6C\">10<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers kasten\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">PowerShell<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>Get-ChildItem somefolder | Select -Last 10<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #82AAFF\">Get-ChildItem<\/span><span style=\"color: #EEFFFF\"> somefolder <\/span><span style=\"color: #89DDFF\">|<\/span><span style=\"color: #EEFFFF\"> Select <\/span><span style=\"color: #89DDFF\">-<\/span><span style=\"color: #EEFFFF\">Last <\/span><span style=\"color: #F78C6C\">10<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Das geht aber nicht in einem Befehl (oder nur mit einer zu schreibenden Funktion). Damit haben wir das gleiche Problem wie mit head und tail in Unix.<br><\/p>\n\n\n\n<p>Per Get-ChildItem und einem auf die Ausgabe angewendetem Sort Befehl erh\u00e4lt man eine Liste. Diese Ausgabe kann man als Array \/ Liste von Zeichenketten interpretieren und einzelne Elemente mit einen nachfolgenden Index in eckigen Klammern ausw\u00e4hlen.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers kasten\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">PowerShell<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>(Get-Content hashes.csv)&#91;1&#93;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #89DDFF\">(<\/span><span style=\"color: #82AAFF\">Get-Content<\/span><span style=\"color: #EEFFFF\"> hashes.csv<\/span><span style=\"color: #89DDFF\">)&#91;<\/span><span style=\"color: #F78C6C\">1<\/span><span style=\"color: #89DDFF\">&#93;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Der &#8220;Trick&#8221; besteht darin, dass eine solche Powershell Liste auch einen Bereich adressieren kann und dieser Bereich &#8220;\u00fcberlaufen&#8221; darf und dann von vorn anf\u00e4ngt. <em>[0..10]<\/em> gibt die ersten 11 Elemente aus. <em>[10..-1]<\/em> gibt alle Zeilen ab der 11. Zeile bis zur letzten (-1) aus.<\/p>\n\n\n\n<p><em> [-10..10]<\/em> gibt die letzten&nbsp; 10 und die ersten 11 Zeilen&nbsp; aus (da 0 die erste Zeile ist). Das Ergebnis sieht etwas ungew\u00f6hnlich aus, da zuersrt die letzten Zeilen ausgegeben werden: <br><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers kasten\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">PowerShell<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>Get-ChildItem $folder *jpg | Sort-Object LastWriteTime)&#91;-10..10&#93;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #82AAFF\">Get-ChildItem<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">$<\/span><span style=\"color: #EEFFFF\">folder <\/span><span style=\"color: #89DDFF\">*<\/span><span style=\"color: #EEFFFF\">jpg <\/span><span style=\"color: #89DDFF\">|<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">Sort-Object<\/span><span style=\"color: #EEFFFF\"> LastWriteTime)<\/span><span style=\"color: #89DDFF\">&#91;<\/span><span style=\"color: #F78C6C\">-10<\/span><span style=\"color: #89DDFF\">..<\/span><span style=\"color: #F78C6C\">10<\/span><span style=\"color: #89DDFF\">&#93;<\/span><\/span><\/code><\/pre><\/div>\n\n\n<p>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"424\" src=\"https:\/\/techno.slomka.biz\/wp-content\/uploads\/2018\/08\/PSTailHead.png\" alt=\"\" class=\"wp-image-502\" srcset=\"https:\/\/techno.slomka.biz\/wp-content\/uploads\/2018\/08\/PSTailHead.png 601w, https:\/\/techno.slomka.biz\/wp-content\/uploads\/2018\/08\/PSTailHead-300x212.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption class=\"wp-element-caption\">Dier zweite bis zehnte Zeile der Datei und die letzten sind sind in den blauen Rechtecken hervorgehoben, dier erste Zeile der Ausgabeliste ist in der Mitte.<\/figcaption><\/figure>\n\n\n<\/p>\n\n\n<p>Dieses Verfahren funktioniert f\u00fcr alle Listausgaben, wie Get-ChildIten, Get-Content, Get-Prozess und so weiter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related posts<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/techno.slomka.biz\/?p=1018\" data-type=\"link\" data-id=\"https:\/\/techno.slomka.biz\/?p=1018\">\ud83d\udca1How to Extract Specific Lines from a File: sed, awk, head\/tail, grep, and with powershell<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/techno.slomka.biz\/?p=494\" data-type=\"link\" data-id=\"https:\/\/techno.slomka.biz\/?p=494\">\ud83d\udca1PowerShell Tip for Large Files \u2013 The Get-Content -Tail and -Wait parameters<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/techno.slomka.biz\/?p=490\" data-type=\"link\" data-id=\"https:\/\/techno.slomka.biz\/?p=490\">\ud83d\udca1Powershell Tip: Equivalent to Unix &#8216;ls -ltr | tail&#8217;<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Ausgabe der letzten und der ersten n-Zeilen in einemBefehl In Unix\/Linux Derivaten sind die Befehle tail und Head oft benutzte Werkzeuge zur Ausgabe der ersten\/letzten Zeilen einer Datei oder der Ergebnisse eines Befehls. Direkt kombinieren kann man sie nicht. Hier sind sed\/awk und andere Werkzeuge und L\u00f6sungen gefordert. Powershell bietet eine sehr effiziente L\u00f6sung. Der erste Ansatz w\u00e4re die Ausgabe mit Select -Last und Select -First zu behandeln. Das geht aber nicht in einem Befehl (oder nur mit einer zu schreibenden Funktion). Damit haben wir das gleiche Problem wie mit head und tail in Unix. Per Get-ChildItem und einem auf die Ausgabe angewendetem Sort Befehl erh\u00e4lt man eine Liste. Diese Ausgabe kann man als Array \/ Liste von Zeichenketten interpretieren und einzelne Elemente mit einen nachfolgenden Index in eckigen Klammern ausw\u00e4hlen. Der &#8220;Trick&#8221; besteht darin, dass eine solche Powershell Liste auch einen Bereich adressieren kann und dieser Bereich &#8220;\u00fcberlaufen&#8221; darf und dann von vorn anf\u00e4ngt. [0..10] gibt die ersten 11 Elemente aus. [10..-1] gibt alle Zeilen ab der 11. Zeile bis zur letzten (-1) aus. [-10..10] gibt die letzten&nbsp; 10 und die ersten 11 Zeilen&nbsp; aus (da 0 die erste Zeile ist). Das Ergebnis sieht etwas ungew\u00f6hnlich aus, da zuersrt die letzten Zeilen ausgegeben werden: Dieses Verfahren funktioniert f\u00fcr alle Listausgaben, wie Get-ChildIten, Get-Content, Get-Prozess und so weiter. Related posts<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[78],"class_list":["post-501","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell"],"_links":{"self":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=501"}],"version-history":[{"count":6,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":1177,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/501\/revisions\/1177"}],"wp:attachment":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}