.+++++. .--. .--. .--.--.--. | ~~~~~ | .oO( Programmieren, Ich? ) ) '*_*' ( `-----' `------' `--' ( ._. ) '.._..' Workshop/Vortrag von _,/\ /\,_ Andreas Romeyke (romeyke@cbs.mpg.de) / ':' \ ___ _ _ _ _ / __| __ | |_ _ _ ___ | || | ___ _ _ ___ ___ (_) _ _ \__ \/ _|| ' \ | ' \ / -_)| || |/ -_)| '_| (_- file3 | $ cat file3 | this is content of file1 | this is content of file2 `--------------------------- Man kann auch anhängen mit >> .--------------------------- | $ cat file3 | this is content of file1 | this is content of file2 | $ cat file1 >> file3 | $ cat file3 | this is content of file1 | this is content of file2 | this is content of file1 `--------------------------- cat kann aber mehr. Zum Beispiel: Zeilennummerierung .--------------------------- | $ cat -n file1 | 1 this is content of file1 `--------------------------- oder nichtdruckbare Zeichen anzeigen .--------------------------- | $ cat -A file1 | this^I is content of file1 $ `--------------------------- cat schreibt alles mit einmal auf Konsole. more und less sind Pager, man kann navigieren, suchen etc. .--------------------------- | $ cat file1 | more `--------------------------- oder .--------------------------- | $ cat file1 | less `--------------------------- Suchen geht mit /, beenden mit q Was ist besser, less oder more? Weniger ist mehr... :) Aller Anfang... Ich will doch aber nur den Anfang einer Datei sehen .--------------------------- | $ cat -n file3 | head -n 1 | 1 this is content of file1 `--------------------------- Nein, das Ende! .--------------------------- | $ cat -n file3 | tail -n 1 | 3 this is content of file1 `--------------------------- Lieber alles Rückwärts... .--------------------------- | $ cat -n file3 | tac | 3 this is content of file1 | 2 this is content of file2 | 1 this is content of file1 `--------------------------- Schere... Wozu Excel und Co? Unter Linux gibt es Schere und Klebstoff. _ _ (_) / ) | (_/ _+/ //|\ cut -d DELIMITER -f FIELD // | ) (/ |/ .--------------------------- | $ cat file3 | cut -d " " -f 1,5 | this file1 | this file2 | this file1 `--------------------------- ... und Klebstoff ___________ || \___ || Leim |__| Verschnippelt? ||__________/ o join file1 file2 .--------------------------- | $ join file1 file2 | this is content of file1 is content of file2 `--------------------------- Suchen... ,-. ( # )==o mit grep `-' .--------------------------- | $ cat -n file3 | grep file1 | 1 this is content of file1 | 3 this is content of file1 `--------------------------- Ersetzen Nicht mehr ganz so leicht. Dazu benötigen wir den Streameditor sed. Aber der kann fast alles... :) Einfaches Wort ersetzen: .--------------------------- | $ cat file1 | sed s/content/Inhalt/g | this is Inhalt of file1 `--------------------------- Alle "o" durch "e" und alle "e" durch "o" ersetzen: .--------------------------- | $ cat file1 | sed y/eo/oe/g | this is centont ef filo1 `--------------------------- Alle Leerzeichen durch Tabulatoren ersetzen: .--------------------------- | $ cat file1 | sed y/ +/\t/g | this is content of file1 `--------------------------- Alle Ziffern verdoppeln: .--------------------------- | $ cat file1 | sed s/\d/\1\1/g | this is Inhalt of file11 `--------------------------- Kann ich auch zählen? Jep. Dafür gibt es 'wc', den Wortzähler .--------------------------- | $ cat file3 | wc | 3 15 75 | $ cat file3 | wc -l | 3 | $ cat file3 | wc -w | 15 | $ cat file3 | wc -c | 75 `--------------------------- Wie kann ich zählen, wie oft Wortteil 'is' vorkommt? .--------------------------- | $ cat file3 | sed -e 's/is/\nis\n/g'| grep is| wc -l | 6 `--------------------------- Sortieren .--------------------------- | $ cat file3 | sort | this is content of file1 | this is content of file1 | this is content of file2 `--------------------------- Numerisch geht auch: sort -n Man kann auch Spalten bestimmen, nach denen sortiert werden soll: sort --key=STARTPOS-ENDPOS Wenn eine Datei sortiert vorliegt kann man auch gleiche Zeilen löschen lassen .--------------------------- | $ cat file3 | sort | uniq | this is content of file1 | this is content of file2 `--------------------------- Oder alle doppelten Zeilen anzeigen .--------------------------- | $ cat file3 | sort | uniq -d | this is content of file1 | this is content of file1 `--------------------------- Reise durch die Welt der Textkodierungen Windows, Linux und Macintosh haben unterschiedliche Zeilenende-Zeichen - Linux: Newline (\n) - Windows: Carriage Return + Newline (\r\n) - Macintosh: Carriage Return (\r) Umwandeln mit .--------------------------- | $ cat file1 | unix2dos > file.dos | $ cat -A file.dos | this is content of file1^M$ `--------------------------- Alternativ geht es auch mit dem Streameditor sed. Mehr Infos im Wiki unter http://cnswiki/bin/view/EDV/FuerUser/ZeilenendeCodierung Dateinamen, erm, verdingsen Mit 'basename' kann man Endungen wegschneiden .--------------------------- | $ basename test.bmp .bmp | test `--------------------------- ... aber auch Pfadanteile .--------------------------- | $ basename /tmp/blubberblase | blubberblase `--------------------------- Wozu man das braucht? Antwort kommt gleich Mit 'ls' kann man sich Inhalte und Verzeichnisse auflisten lassen. Aber, was für eine Datei es sich handelt sieht man nicht. Hier hilft 'file' .--------------------------- | $ file * | file.dos: ASCII text, with CRLF line terminators | schneller_sein.tex: UTF-8 Unicode text | schneller_sein.tpp: UTF-8 Unicode C++ program text | test1.txt: ASCII text | test2.txt: ASCII text `--------------------------- Man sollte nicht alles für bare Münze nehmen! Schleifenmagie Ja, unter der Bash kann man richtig programmieren. In unserem ~/pics/ seien viele, sehr viele, tausende Bitmap-Dateien. Wir benötigen aber JPGs, was nun? .--------------------------- | ~/pics$ ls | head -n 2 | 1.bmp | 2.bmp | ~/pics$ for i in *.bmp; do j=$(basename "$i" .bmp).jpg; echo "convert $i to $j"; | convert "$i" "$j"; done | convert 1.bmp to 1.jpg | convert 2.bmp to 2.bmp | [..] `--------------------------- _.--._ _.--._ ,-=.-":;:;:;\':;:;:;"-._ \\\:;:;:;:;:;\:;:;:;:;:;\ im Wiki \\\:;:;:;:;:;\:;:;:;:;:;\ \\\:;:;:;:;:;\:;:;:;:;:;\ Linuxbrochüre \\\:;:;:;:;:;\:;::;:;:;:\ \\\;:;::;:;:;\:;:;:;::;:\ 'man COMMAND' \\\;;:;:_:--:\:_:--:_;:;\ \\\_.-" : "-._\ \`_..--""--.;.--""--.._=> " _ _ _ ___ ___ | \| | ___ __ | |_ | __|_ _ __ _ __ _ ___ _ _ |__ \ | .` |/ _ \/ _|| ' \ | _|| '_|/ _` |/ _` |/ -_)| ' \ /_/ |_|\_|\___/\__||_||_| |_| |_| \__,_|\__, |\___||_||_|(_) |___/