Search code examples
regexawkfindconditional-statementsskip

awk - condition find line by regex / string and delete last line


I got a convoluted transaction csv. I need to extract data that begins at a certain line / field containing a string "Transaktion" and also to skip the last line of (each) csv.

I can make AWK find the starting line and process then all following. That is fine, but I can't add a way to skip last line.

/^Transaktionen/,/^$/ {
getline; 
print $2 FS $3 FS $4} 

Solution

  • You can use this awk:

    awk '/^Transaktionen/{p=1; next} p {
       if (c2 != "") print c2, c3, c4; c2=$2; c3=$3; c4=$4}' file.csv
    

    Try this awk script:

    BEGIN {
            #OFS=FS=";" ;
            #print $0;
            print "Datum2;Zahlart3;TXN Kundendetails4 - KundenID5;Beschreibung6;Betrag7;Gebuehr10;Disagio11;Auszahlungsbetrag12";
            #einnahmen = "Transaktionen";
            #erstattung = "Refunds";
    }
    /^Transaktionen/{p=1; next} p {
       if (c2 != "")
          print c2, c3, c4, c5, c6 , c7 ,c10, c11, c12;
       c2=$2; c3=$3; c4=$4; c5=$5; c6=$6; c7=$7; c10=$10; c11=$11; c12=$12;
    }