grep, sed, tail and tee
Contents
grep
grep is a command-line utility for searching plain-text data sets for lines matching a regular expression. Grep was originally developed for the Unix operating system, but is available today for all Unix-like systems. (Wikipedia)
Search all lines in the file, that contain hello
grep hello example.txt
Search all lines in the file, that does not contain hello. And in the second case world is ignored as well.
grep -v hello example.txt
grep -Ev "hello|world"
Search in directory
grep -r hello exampleDir/*
Search recursive for files with the extension .txt containing the string "example" and excluding directories named vendor
grep -r --include=*.txt --exclude-dir=vendor 'example'
Examples
Grep lines, that contain the string test. Additional grep 10 lines before (-B
) and 5 lines after (-A
) this lines and print the line numbers (-n
):
grep -A 5 -B 10 -n 'test' example.txt
Get hex-/ binary characters
Search lines containing the character 0x00 (NULL)
grep -P '\x00' example.txt
Usage in combination with other tools
sed
In combination with sed
grep hello example.txt | sed -e 's/.*Result:: //' > new_file.txt
tail
In combination with tail
tail -fn 20 example.txt | grep --line-buffered hello
sort
grep hello example.txt | sort | uniq -c | sort -n
sed
sed steht für Stream EDitor und ist ein Unix-Werkzeug, mit dem Text-Datenströme bearbeitet werden können. Der Datenstrom kann auch aus einer Datei gelesen werden. Im Gegensatz zu einem Texteditor wird die Ursprungsdatei aber nicht verändert. (Wikipedia)
Zeichenketten entfernen / ersetzen
Ersetzt alt durch neu
sed -e 's/alt/neu/' example.txt
Entfernt alt
sed -e 's/alt//' example.txt
Entfernt in jeder Zeile alle Zeichen ab (und einschließlich) AB
sed -e 's/AB.*//' example.txt
Entfernt in jeder Zeile alle Zeichen vor (und einschließlich) AB
sed -e 's/.*AB//' example.txt
Output specific line
Get for instance the 3. line of a file
sed '3!d' example.txt
Zeilen entfernen
Entfernt alle Zeilen, die EXAMPLE enthalten
sed -i '/EXAMPLE/d' example.txt
Kombination mehrere Befehle
Beispiel
sed -e 'BEFEHL-1' -e 'BEFEHL-2' example.txt
tail
tail [...] ist einer der vielen Textfilter in Unix [...] dient zur Ausgabe der letzten Zeilen einer Datei. (Wikipedia)
Gibt die letzten Zeilen 20 Zeilen der Datei aus:
tail -n 20 example.txt
Gibt fortlaufend die letzten Zeilen 20 Zeilen der Datei aus:
tail -fn 20 example.txt
In Kombination mit tee und sed
tail -fn 20 example.txt | tee new_file.txt | sed -e 's/;0x.*//'
In Kombination mit grep
tail -fn 20 example.txt | grep --line-buffered 0x0A
tee
tee ist ein Standard-Unix-Befehl. [...] Der Befehl liest Daten von stdin und gibt sie nach stdout und in eine (neu anzulegende oder bestehende) Datei aus. Er wird benutzt, um Zwischenergebnisse innerhalb einer Pipe zu erhalten. (Wikipedia)
Schreibt die Konsolenausgaben zusätzlich in die Datei
bash BASH_SCRIPT | tee new_file.txt
In Kombination mit tail und sed
tail -fn 20 example.txt | tee new_file.txt | sed -e 's/AB.*//'