Batch Zählenwennfunktion in txt einfügen

@simpsonsfan:

Das könnte ein Versionsproblem sein. Ich verwende:
Code:
C:\Users\User\Work>awk --version
GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.2-p10, GNU MP 6.0.0)
Copyright (C) 1989, 1991-2014 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

Es fängt damit an, dass das erste ' (Hochkomma) bemäkelt wird und dann fängt sich der Interpreter nicht mehr ein. Ich kompiliere mir awk selber, allerdings nicht statisch, so dass ich es nicht hochladen kann. Ich empfehle, es mit gawk aus dieser Distribution zu versuchen.
 
Moin, danke für die ganzen Posts :)

simpsonsfan schrieb:
Ich habe gawk for Windows 3.1.6-1 verwendet, von http://gnuwin32.sourceforge.net/packages/gawk.htm

Aufruf ganz normal aus der cmd heraus, nicht als Batch.
Anhang anzeigen 581983

Genau die selbe Fehlermeldung bekomme ich auch aus der cmd heraus.

Ich habe folgende Version:
Code:
GNU Awk 4.1.3, API: 1.1
Copyright (C) 1989, 1991-2015 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

also eine etwas neuere als @Endoro, jedenfalls nach der Version her.

Habe mal ein paar andere Sachen aus dem Netz bzgl. "printf" probiert und keine hat funktioniert. Jedes mal bekomme ich den Fehler mit "Backslash". Wenn ich das "\n" dann weglasse, gibt es gar nichts mehr aus.

EDIT:

Ich habe den folgenden Code jetzt mal ausprobiert:
Code:
@echo on & setlocal
gawk -F";" "{ if (NR==1) {header= "Freq;" $0} else {d[NR]=$0; n[$1]++} next}; END {print (header); for (a in d) {$0=d[a]; print(n[$1],d[a])}}" in.csv > out.csv

Bekomme in die out.csv folgende Daten:
Code:
2 Peter;Adresse1;0815
2 Peter;Adresse1;1234
1 Karsten;Adresse2;5678
3 Helmut;Adresse3;3456
3 Helmut;Adresse3;2958
3 Helmut;Adresse3;1057

Die Anzahl ist schonmal da, jedoch nicht mit Semikolon getrennt.
Weiterhin fehlen die Überschriften (header).
Ergänzung ()

So habe es jetzt hinbekommen.
Habe es mit "print" geschafft anstelle von "printf". Vorher noch den Outputdelimiter auf ";" festgelegt und die Ausgabe ist so, wie ich es mir gewünscht habe.

Hier die Codezeile:
Code:
@echo on & setlocal
gawk -F";" -v OFS=";" "{ if (NR==1) {header= "Freq;" $0} else {d[NR]=$0; n[$1]++} next}; END {for (a in d) {$0=d[a]; print(n[$1],d[a])}}" Temp2.txt > Temp3.txt

Danke an alle, die mir hier geholfen haben!

Grüße,
David
 
Zuletzt bearbeitet: (Ergänzung neuer Informationen)
@Dave1701: Das ist ein Problem mit dem Quoting unter Windows. Unterschiedliche gawk-Distributionen scheinen das unterschiedlich zu handhaben. Mit dem folgenden Batchcode solltest du zum Ziel kommen:

Code:
@echo on &setlocal
gawk -F";" -v OFS=";" "{if (NR == 1) {header = \"Freq;\" $0} else {d[NR]=$0; n[$1]++}} END {print header; for (a in d) {$0 = d[a]; print n[$1], d[a]}}" in.csv
Ergänzung ()

Man kann das Problem mit der Quotierung umgehen, wenn man awk-Programme erstellt. Darin wird immer nach Unix quotiert, damit die Programme plattformübergreifend einsetzbar sind. Im Beispiel hier könnte man ein awk-Programm "freq.awk" erstellen, was so aussieht:
Code:
#!/bin/awk

BEGIN {
	FS = ";"
	OFS = FS
}

{
	if (NR == 1) {
		header = "Freq;" $0
	}
	else {
		d[NR]=$0
		n[$1]++
	}
}

END {
	print header
	for (a in d) {
		$0 = d[a]
		print n[$1], d[a]
	}
}

Das würde man folgendermaßen aufrufen:
Code:
gawk -f freq.awk in.csv > out.csv
 
Zuletzt bearbeitet: (Code in der Batch gestrafft)
Zurück
Oben