Textdatei umformatieren

Post Reply
Message
Author
eq

Textdatei umformatieren

#1 Post by eq »

hi,

ich habe eine Textdatei mit folgendem Inhalt:

User;Passwort;Datum;000500;800000;
Namx;FileExtr;Datum;000780;600000;
Name;Beschrei;Datum;000500;240540;

und ich brauche diese Datei mit folgender Formatierung:
80;500;000500.txt
60;780;000780.txt
24;500;000500.txt

derVOrteil hier ist, dass die Anzahl der Stellen in der Quelldatei in allen Zeilen identisch ist.

wie kann ich eine Textdatei zeilenweise auslesen,
jede dieser zeile konvertieren und in eine neue Textdatei schreiben.

eine Zeile "User;Passwort;Datum;000500;800000;" in "80;500;000500.txt" zu ändern ist ja mit "cut" kein Problem. Nur wie gehe ich eine Datei zeilenweise durch und ändere das ?


DANKE

Descartes

Re: Textdatei umformatieren

#2 Post by Descartes »

Nimm doch ein AWK Script:

Aufruf:
gawk -f script.awk input.txt > output.txt

Inhalt von "script.awk":
<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">
BEGIN { FS=";";RS="\<!--no-->n"; }
{
spalte1 = substr($5,1,2);
spalte2 = substr($4,length($4)-2,3);
spalte3 = $4;
printf("%s;%s;%s.txt\<!--no-->n", spalte1, spalte2, spalte3);
}
</font><hr></pre></blockquote>

eq

Re: Textdatei umformatieren

#3 Post by eq »

hi .. danke .. wie kann ich das in ein Shellscript einbetten, so dass die Eingabaparameter $1 und $2 als input.txt und als output.txt behandelt werden ?


DANKE

Jochen

Re: Textdatei umformatieren

#4 Post by Jochen »

<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">gawk '
BEGIN { FS=";";RS="
"; }
{
spalte1 = substr($5,1,2);
spalte2 = substr($4,length($4)-2,3);
spalte3 = $4;
printf("%s;%s;%s.txt
", spalte1, spalte2, spalte3);
}' "$1" > "$2"</font><hr></pre></blockquote>Natürlich sollte man dann sauber arbeiten und überprüfen, ob man überhaupt zwei Argumente übergeben bekommen hat usw. Aber so geht es prinzipiell.

Jochen

eq

Re: Textdatei umformatieren

#5 Post by eq »

DANKE .. vielen DANK

Post Reply