text files bearbeiten

Started by kris99, 2011/12/14, 14:46:18

Previous topic - Next topic

kris99

Hallo Zusammen,

ich stehe gerade vor der Aufgabe eine csv-Datei mit einer String-Datei abzugleichen.

Konkret: wird ein Wort oder Zeile aus der String-Datei in einem beliebigen Feld in der csv-Datei gefunden, wird diese Zeile aus der csv-Datei gelöscht.

Ich habe vor das mit Perl zu lösen, bin aber auf dem Gebiet Neuling. D. h. ich muß mich erst in Perl einarbeiten (was nicht schwierig sein sollte) und ggf. das entsprechende Modul finden.

Hat einer von Euch schon mal etwas ähnliches gemacht und wenn ja womit?

Gruß

Kris

cryptosteve

Kurze Frage vorweg: ist die Verwendung von perl zwingende Voraussetzung oder hast Du das jetzt hier nur genannt, weil es Dir als möglicherweise beste Lösung erscheint?
- born to create drama -
CS Virtual Travel Bug: VF6G5D

kris99

Perl ist keine Voraussetzung, schien mir aber mit meinem Wissen die praktikabelste Lösung, z. B. mittels hash.

Das Ganze in der bash mit grep und awk zu scripten ginge auch, wird aber wahrscheinlich schnell unübersichtlich.

cryptosteve

Hmm ... ich dachte, sed wäre die einfachere Lösung und ich könnte die Lösung zügig aus der Hüfte schießen. Letzteres war leider ein Irrtum, da muss ich noch etwas lesen und knobeln.
- born to create drama -
CS Virtual Travel Bug: VF6G5D

kris99

Wenn Du das in sed hin bekommst, bist Du der "sed"-Gott!

cryptosteve

Die Frage ist jetzt, ob Du die strings-Datei selbst erstellen/ändern kannst.

Um dem ganzen mal etwas Inhalt zu verpassen:

[stell @ shangrila:~]% cat strings
/ich/d
/boo/d


[stell @ shangrila:~]% cat csv
csv;ist;datei;toll
hans;wurst;isis;baerbel
foo;bar;ich;test;test
toll;tst;test;test;
foo;voo;woo;boo
bar;bar;bahr;dies
datei;datei;datei;datei;


[stell @ shangrila:~]% sed -f strings csv
csv;ist;datei;toll
hans;wurst;isis;baerbel
toll;tst;test;test;
bar;bar;bahr;dies
datei;datei;datei;datei;
- born to create drama -
CS Virtual Travel Bug: VF6G5D

kris99

So ähnlich, die Aufgabe hast Du richtig verstanden. Die string-Datei existiert. Pro Zeile ein Name. Also so:

> cat strings
yelp
centos
yast2


Die csv-Datei sieht so aus:

> cat csv
"yast2-x11";"";"2.9.11-0.3";"";"Global license Installations: 1";
"yast2-xml";"";"2.9.8-19.2";"";"Global license Installations: 1";
"yast2-you-server";"";"2.9.17-0.1";"";"Global license Installations: 1";
"yelp";"";"2.6.4-2 2.14.3-2 2.2.3-1.E 2.16.0-22.el5";"";"Global license Installations: 21";
"yp-tools";"";"2.8-7 2.8-1 2.8-188.1 2.8-6 2.9-0.1";"";"Global license Installations: 18";
"ypbind";"";"1.17.2-8 1.17.2-13 1.18-1.7 1.12-5.21.9 1.19-11.el5 1.17.2-17.el4";"";"Global license Installations: 18";
"ypserv";"";"2.13-19.el4_8.2 2.8-21 2.12.1-44.5 2.13-19.el4_8.1";"";"Global license Installations: 4";
"yum";"";"2.4.2-4 2.4.2-5 3.2.19-18.el5_3.1 3.2.8-9.el5.centos.2.1 3.2.22-26.el5.centos";"";"Global license Installations: 16";


Und das Ergebnis soll so aussehen:

> cat csv
"yp-tools";"";"2.8-7 2.8-1 2.8-188.1 2.8-6 2.9-0.1";"";"Global license Installations: 18";
"ypbind";"";"1.17.2-8 1.17.2-13 1.18-1.7 1.12-5.21.9 1.19-11.el5 1.17.2-17.el4";"";"Global license Installations: 18";
"ypserv";"";"2.13-19.el4_8.2 2.8-21 2.12.1-44.5 2.13-19.el4_8.1";"";"Global license Installations: 4";

cryptosteve

Achso, da reicht dann wohl sogar grep.

[stell @ shangrila:~]% grep -v -F -f strings csv
"yp-tools";"";"2.8-7 2.8-1 2.8-188.1 2.8-6 2.9-0.1";"";"Global license Installations: 18";
"ypbind";"";"1.17.2-8 1.17.2-13 1.18-1.7 1.12-5.21.9 1.19-11.el5 1.17.2-17.el4";"";"Global license Installations: 18";
"ypserv";"";"2.13-19.el4_8.2 2.8-21 2.12.1-44.5 2.13-19.el4_8.1";"";"Global license Installations: 4";
- born to create drama -
CS Virtual Travel Bug: VF6G5D

kris99

Danke! Damit komme ich weiter.

Manchmal ist die Lösung doch so einfach  :oops: