• 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…

  • Debian,  Linux,  RedHat,  Ubuntu

    Identifying the Process on an Open Port

    There are several ways to find the process on an open port. As an example, let’s find out which process is running on port 5000. The first attempt could be to use `grep` on the port with ps -ef. This approach is only successful if a process receives the port as an argument. This is only the case in a few instances. LSOF The lsof command with the -i option followed by :<PORT> displays the processes that are keeping this port open. đź’ˇIf you are not the technical user under which the process is running, you will not see any output. Using sudo or as the user who owns the…

  • Korn Shell / Bash

    Zeichen aus Zeichenketten mit bash/ksh schneiden

    Mit bash oder Korn Shell kann man auf einfachem Wege Zeichen in Zeichenketten ermitteln, bzw. herausschneiden. Dazu ist einfach nach einer Variable mit : getrennt die erste zu ermittlende Position anzugeben, gefolgt von einem weiteren : mit der Anzahl der auszugebenden Zeichen. ${VARIABLENAME:<erstes Zeichen>:<Anzahl>} Beispiel: TEXT="Holger Slomka" echo ${TEXT:2:3} lge Ein Beispiel fĂĽr eine mögliche Verwendung, ist die Ausgabe aller Zeilen einer Datei, die nicht mit # beginnen. for Zeile in `cat inifile| cut -d’ ‘ -f1`; do if [[ ${Zeile:0:1} != “#” ]]; then   echo $Zeile/span> fi done Es werden also Kommentarzeilen ausgeblendet, analog grep -v ^#.

  • Korn Shell / Bash,  Shell Scripting

    Sehr nĂĽtzliche grep Funktionen

    grep wird von Linuxx/Unix Administratoren zwar sehr oft genutzt, einige sehr mächtige Optionen werden aber zu Unrecht wenig genutzt oder sind den Administratoren nicht bekannt. 1. Zeilen vor und nach der Fundstelle des Suchbegriffs ausgeben. grep bietet die äusserst nĂĽtzliche Funktion, Zeilen vor und nach dem gefundenen Suchbegriff anzuzeigen. Um die Funktionen der grep Optionen darzustellen, verwende ich als Beispiel ein IBM Skript mit folgendem Inhalt, das von grep durchsucht wird: cat collector.sh #!/bin/sh binDir=`dirname ${0}` . ${binDir}/setupCmdLine.sh ${WAS_HOME}/bin/collector.sh “$@” Die Option -A zeigt n Zeilen nach der Fundstelle an. grep -A 1 setup collector.sh . ${binDir}/setupCmdLine.sh ${WAS_HOME}/bin/collector.sh “$@” Die Option -B zeigt n Zeilen vor der Fundstelle an. grep…

  • Korn Shell / Bash,  Shell Scripting

    Zeichenketten in mehreren Dateien und Verzeichnissen suchen

    Unter Unix bietet grep eine einfache Lösung um Zeichenketten in Dateien zu finden. Möchte man in allen Dateien eines Verzeichnisses und den Unterverzeichnissen eine bestimmte Zeichenkette (z.B BLABLA) suchen, so reicht der Befehl grep BLABLA * nicht, da keine Unterverzeichnisse durchsucht werden. Eine Alternative ist die Kombination aus den Befehlen: find, xargs und grep. find . -type file | xargs grep -i BLABLA find . -type file -print | xargs grep -i BLABLA egrep ist eine weitere mächtige Alternative. Mit der Option -R ist es in der Lage rekursiv Unterverzeichnisse zu durchsuchen: egrep -R BLABLA * Das funktioniert auch Super in der Windows bash! Allerdings kann man grep nicht auf…