• Git,  Korn Shell / Bash,  PowerShell,  Windows Bash

    Quick CRLF-to-LF Conversion in PowerShell Without `dos2unix`

    Although this topic has been discussed so often, I think I can still contribute something regarding CRLF/LF. You write a shell script on Windows, copy it to a Linux server, run it – and get: (Newer bash versions are even less helpful: cannot execute: required file not found.) The culprit is Windows line endings: **CRLF** (\r\n) instead of **LF** (\n). Bash treats the trailing \r as part of the command. If dos2unix is not at hand, PowerShell handles the conversion in a single line that isn’t particularly easy to remember: ## How it works – [IO.File]::ReadAllText($p) reads the whole file into a single string – line endings included. – -replace…

  • Korn Shell / Bash,  PowerShell,  Shell Scripting,  Windows Bash

    💡How to Extract Specific Lines from a File: sed, awk, head/tail, grep, and with powershell

    In the command line, it’s often necessary to extract only specific lines from a text file – whether it’s a single line or a range of lines. Fortunately, Bash offers several powerful tools for this. In this blog post, I’ll show you the most common methods using sed, awk, head/tail, and grep. Additionally, I will demonstrate an option when using PowerShell. Extracting Lines with sed sed (Stream EDitor) is a classic for text manipulation in the shell and is excellent for selectively outputting line ranges.Output a Single Line (e.g., Line 102)To output a specific line, such as line 102, you use the p command (print) in combination with the -n…

  • PowerShell,  Shell Scripting,  Uncategorized

    💡Powershell Tip – Search directories and subdirectories for files with specific content

    I’d like to share a small PowerShell script that scans all subdirectories for specific files (for example, *.yaml and *.yml) and searches them for a specific string (e.g., “ansible.builtin.template”). The result is returned with the location (name and path of the file, and line number of the search string in the file). What it does: Sometimes I want to save my results to a file and explore them further. To save the results to a CSV file, adapt the script as follows:

  • PowerShell

    💡Powershell Tip: Tail und Head vereint

    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ösungen gefordert. Powershell bietet eine sehr effiziente Lösung. Der erste Ansatz wäre 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ält man eine Liste. Diese…

  • PowerShell,  Windows Bash,  WSL

    💡PowerShell Tip for Large Files – The Get-Content -Tail and -Wait parameters

    In PowerShell, you can display the last *n* lines of large text or ASCII files using the `-Tail` parameter of the `Get-Content` cmdlet: In Powershell, you can display the last *n* lines of large tyt or ASCII files using the -Tail parameter of the Get-Content cmdlet: Since PowerShell version 3, Get-Content (or its alias gc) includes the -Tail parameter. Unfortunately, there is no built-in equivalent to the Unix tail -f command for continuous updates. PowerShell: Get-Content with -Wait PowerShell’s Get-Content cmdlet supports the -Wait parameter, which continuously monitors a file for new content: This will display new lines as they are appended to the file. PowerShell with filtering (advanced usage)…

  • PowerShell,  Windows

    PowerShell Drive alias:

    PowerShell nutzt das Konzept der Laufwerke (=PowerShell Drives) sehr ausgiebig. So werden die Umgebungsvariablen in env: verwaltet, die Windows Registry aber auch die “normalen” Laufwerke wie “C:”. Auch Aliasse für Kommandos sind ein PowerShell Drive sind. Get-ChildItem alias:

  • PowerShell,  Windows

    Mehrfache Einträge in großen Konfigurationsdateien oder Protokollen finden (Windows Powershell Lösung)

    Im Post Mehrfache Einträge in großen Konfigurationsdateien oder Protokollen finden(Linux/Unix) habe ich gezeigt, wie mehrfach vorkommende Zeilen in Textdateien gefunden und automatisch entfernt werden können. Windows hat im Zuge der Powershell ein mächtiges Werkzeug, dass dies auch kann. (Seit dem Windows 10 1709 Update kann man natürlich auch einfach die eingebaute bash nehmen….) 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ählt man wie mit Group-Object Get-Content -Path…