Hallo! Ich schreibe mir gerade ein Überwachungsscript. Da dies nun sehr umpfangreich wird möchte ich eine config Datei hinzufügen, dazu brauch ich aber nun eine Funktion die mir einen String in einer Datei sucht, und den rest danach ausgibt. Z.B. (such nach "IP=") IP=192.168.10.123 wie bekomme ich dort den String "192.168.10.123" heraus? grep liefert mir ja die ganze Zeile, in "man grep" bin ich auch nicht fündig geworden. Schonmal danke im Vorraus! Gruß Mario
Hallo, Mario Ohnewald schrieb am 11.06.2002 (11:47):
wie bekomme ich dort den String "192.168.10.123" heraus? grep liefert mir ja die ganze Zeile, in "man grep" bin ich auch nicht fündig geworden.
grep alleine kann das auch nicht. grep IP | cut -d= -f2 Du schickst das Ergebnis durch eine Pipe an cut, sagst cut, daß = das Trennzeichen ist und daß Du bitte die 2. Spalte haben möchtest. Gruß, Antje -- Women and cats will do as they please, and men and dogs should relax and get used to the idea. - Robert A. Heinlein
"Antje M. Bendrich" wrote:
Mario Ohnewald schrieb am 11.06.2002 (11:47):
wie bekomme ich dort den String "192.168.10.123" heraus? grep liefert mir ja die ganze Zeile, in "man grep" bin ich auch nicht fündig geworden. [...] grep IP | cut -d= -f2
Du schickst das Ergebnis durch eine Pipe an cut, sagst cut, daß = das Trennzeichen ist und daß Du bitte die 2. Spalte haben möchtest.
Um mal eine zweite Möglichkeit zu nennen: awk -F = '/^IP=/ {print $2}' dateiname Im Klartext: '=' als Feldtrenner benutzen und von Zeilen, die mit "IP=" anfangen (d.h. auf den regulären Ausdruck ^IP= passen), das zweite Feld ausgeben. Bei dieser Lösung spart man sich die Pipe, so daß ein Prozeß weniger erzeugt werden muß. So, der nächste kann jetzt z.B. eine Lösung in sed beitragen -- gibt noch reichlich andere Möglichkeiten... ;-) Ein ganz anderer Ansatz, der häufig bei Konfigurationsdateien für Shell-Skripte verwendet wird, wenn alle Konfigurationseinträge die Form Variablenname=Wert haben und jeder Name nur einmal vorkommt: Die Konfigurationsdatei im Skript einfach mit source Dateiname oder kurz . Dateiname einlesen, und schon sind alle Einträge als Shell-Variablen verfügbar. In der Konfigurationsdatei ist dann im Prinzip alles möglich, was die Shell interpretieren kann, also könnte man z.B. neben Variablen auch mittels der Kommandos cd und umask das Arbeitsverzeichnis bzw. die umask des Prozesses festlegen usw. Eilert -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Eilert Brinkmann -- Universitaet Bremen -- FB 3, Informatik eilert@informatik.uni-bremen.de - eilert@tzi.org http://www.informatik.uni-bremen.de/~eilert/
Hallo, On Tue, 11 Jun 2002, Eilert Brinkmann wrote:
Um mal eine zweite Möglichkeit zu nennen:
awk -F = '/^IP=/ {print $2}' dateiname
So, der nächste kann jetzt z.B. eine Lösung in sed beitragen -- gibt noch reichlich andere Möglichkeiten... ;-)
Der Vollstaendigkeit halber z.B. mit: sed -n 's/^IP=\(.*\)/\1/p' datei sed -n '/^IP=/s/^IP="\?\([^"]*\)"\?/\1/p' datei
Ein ganz anderer Ansatz, der häufig bei Konfigurationsdateien für Shell-Skripte verwendet wird, wenn alle Konfigurationseinträge die Form Variablenname=Wert haben und jeder Name nur einmal vorkommt: Die Konfigurationsdatei im Skript einfach mit
source Dateiname
Das ist IMO die beste Methode, v.a. wenn es mehrere Variablen sind, ein einzelnes source (ein bash-builtin) ist um Groessenordnungen schneller als n-mal einen externen Prozess anzuwerfen (egal ob grep, sed oder awk). Ausserdem hat das source den grossen Vorteil, dass man problemlos mehrzeilige Variableninhalte haben kann, z.B: IPS=" 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 " Mit einem Trick kann man dann sogar Zeilenweise kommentieren, z.B: IPS=" 10.0.0.1 # Router 10.0.0.2 # DNS # 10.0.0.3 # DNS2 10.0.0.4 # mailhost " IPS=`echo "$IPS" | sed 's/#.*//g' | xargs echo` Und obendrein lassen sich shell-funktionen definieren ;) -dnh -- 102: Code Reuse (cat a.out_header; cat) > a.out (Enno Rehling)
Hallo Mario, hallo Leute, Am Dienstag, 11. Juni 2002 11:47 schrieb Mario Ohnewald:
Ich schreibe mir gerade ein Überwachungsscript. Da dies nun sehr umpfangreich wird möchte ich eine config Datei hinzufügen, dazu brauch ich aber nun eine Funktion die mir einen String in einer Datei sucht, und den rest danach ausgibt. Z.B. (such nach "IP=") IP=192.168.10.123
wie bekomme ich dort den String "192.168.10.123" heraus?
Muss es unbedingt grep sein? Gerade für config-Files würde es sich doch anbieten, diese zu "sourcen". Ich gehe mal davon aus, dass Deine Config-Datei configfile heißt. Dazu fügst Du in Deinem eigentlichen Script die folgende Zeile ein: . configfile Wichtig ist der Punkt mit folgendem Leerzeichen am Zeilenanfang. Danach hast Du (um bei Deinem Beispiel zu bleiben) in $IP eben 192.168.0.123 stehen. Mit echo $IP kannst Du das Ganze kontrollieren. Ansonsten: man bash ;-) Zu Deiner anderen Mail: Tabstopps werden mit \t maskiert, das sollte grep, cut u. a. verstehen. Wenn Du sicherstellst, dass in den einzelnen Feldern keine Leerzeichen vorkommen [1], geht es auch einfacher. Zeile=feld1 feld2 feld3 feld4 set -- $Zeile Danach stehen die einzelnen Felder in $1 bis $4 (bei 4 Feldern) Vielleicht helfen Dir auch arrays weiter ( ${Zeile[1]} ) Da ich damit aber noch nicht gearbeitet habe, bleibt nur der Verweis auf man bash. Gruß Christian Boltz [1] Falls doch Leerzeichen vorkommen können, solltest Du Dir mal man bash genauer ansehen, Stichwort diesmal: IFS -- Registrierter Linux-Nutzer #239431 Linux - life is too short for reboots.
participants (5)
-
Antje M. Bendrich
-
Christian Boltz
-
David Haller
-
Eilert Brinkmann
-
Mario Ohnewald