2016-08-02 14 views
0

Ich habe Daten in einer CSV-Spalte, die manchmal Kommas und Zeilenumbrüche enthält. Wenn in meinen Daten ein Komma vorhanden ist, habe ich die gesamte Zeichenfolge in doppelte Anführungszeichen eingeschlossen. Wie würde ich die Ausgabe dieser Spalte in eine TXT-Datei analysieren, wobei die Zeilenumbrüche und Kommas berücksichtigt werden.Awk, um CSV-Spalte mit Kommas und Zeilenumbrüchen zu erhalten

Beispieldaten, die nicht mit meinem Befehl funktioniert:

,"This is some text with a , in it.", #data with commas are enclosed in double quotes 

,line 1 of data 
line 2 of data, #data with a couple of newlines 

,"Data that may a have , in it and 
also be on a newline as well.", 

Hier ist, was ich bisher:

awk -F "\"*,\"*" '{print $4}' file.csv > column_output.txt 
+0

Können Sie doppelte Anführungszeichen in Ihrem Feld mit doppelten Anführungszeichen abgefangen haben, und wenn ja, wie werden sie gemerkt, z. '" foo \ "bar" 'oder' "foo" "bar" 'oder etwas anderes? –

Antwort

0
$ cat decsv.awk 
BEGIN { FPAT = "([^,]*)|(\"[^\"]+\")"; OFS="," } 
{ 
    # create strings that cannot exist in the input to map escaped quotes to 
    gsub(/a/,"aA") 
    gsub(/\\"/,"aB") 
    gsub(/""/,"aC") 

    # prepend previous incomplete record segment if any 
    $0 = prev $0 
    numq = gsub(/"/,"&") 
    if (numq % 2) { 
     # this is inside double quotes so incomplete record 
     prev = $0 RT 
     next 
    } 
    prev = "" 

    for (i=1;i<=NF;i++) { 
     # map the replacement strings back to their original values 
     gsub(/aC/,"\"\"",$i) 
     gsub(/aB/,"\\\"",$i) 
     gsub(/aA/,"a",$i) 
    } 

    printf "Record %d:\n", ++recNr 
    for (i=0;i<=NF;i++) { 
     printf "\t$%d=<%s>\n", i, $i 
    } 
    print "#######" 

.

$ awk -f decsv.awk file 
Record 1: 
     $0=<,"This is some text with a , in it.", #data with commas are enclosed in double quotes> 
     $1=<> 
     $2=<"This is some text with a , in it."> 
     $3=< #data with commas are enclosed in double quotes> 
####### 
Record 2: 
     $0=<,"line 1 of data 
line 2 of data", #data with a couple of newlines> 
     $1=<> 
     $2=<"line 1 of data 
line 2 of data"> 
     $3=< #data with a couple of newlines> 
####### 
Record 3: 
     $0=<,"Data that may a have , in it and 
also be on a newline as well.",> 
     $1=<> 
     $2=<"Data that may a have , in it and 
also be on a newline as well."> 
     $3=<> 
####### 
Record 4: 
     $0=<,"Data that \"may\" a have ""quote"" in it and 
also be on a newline as well.",> 
     $1=<> 
     $2=<"Data that \"may\" a have ""quote"" in it and 
also be on a newline as well."> 
     $3=<> 
####### 

Die oben genannten verwendet GNU awk für FPAT und RT. Ich kenne kein CSV-Format, das es erlaubt, einen Zeilenumbruch in der Mitte eines Felds zu haben, das nicht in Anführungszeichen eingeschlossen ist (wenn Sie nie wissen würden, wo ein Datensatz endet), so dass das Skript dies nicht erlaubt Das. Das obige wurde für diese Eingabedatei ausgeführt:

Verwandte Themen