前の行を現在および補助計算と比較して、既存の.CSVを変換します。

前の行を現在および補助計算と比較して、既存の.CSVを変換します。

さようなら行ってください! 9つの列を持つ.CSVファイルがあります。内容を新しいファイルに解析し、いくつかの簡単な計算を実行し、その過程で新しい行を作成する必要があります。既存の.CSVと目的の出力を表示してこれを説明するのが最善だと思います。

既存の.CSVコンテンツ(9列)

$cat file
Trans #,Type,Date,Num,Name,Memo,Account,Debit,Credit
1321,General Journal,1/4/2013,1127,,Consulting,Cash on Hand,,20.61
,,,,,,IT Services,20.61,
,,,,,,,20.61,20.61
,,,,,,,,
1322,General Journal,7/3/2013,1128,,Utilities,Cash on Hand,,105.5
,,,,,,Utilities,105.5,
,,,,,,,105.5,105.5
,,,,,,,,
1323,General Journal,4/3/2013,1129,,Bell,Cash on Hand,,466.69
,,,,,,Telephone,466.69,
,,,,,,,466.69,466.69
,,,,,,,,
1324,General Journal,1/3/2013,1130,,Consulting,Cash on Hand,,20.61
,,,,,,IT Services,20.61,
,,,,,,,20.61,20.61
,,,,,,,,
1325,General Journal,6/3/2013,1131,,Utilities,Cash on Hand,,79.09
,,,,,,Utilities,79.09,
,,,,,,,79.09,79.09
,,,,,,,,

希望の出力

Trans #,Type,Date,Num,Name,Memo,Account,Debit,Credit
1321,Expense,1/4/2013,1127,0,Consulting,Cash on Hand,,20.61
1321,Expense,1/4/2013,1127,0,Consulting,IT Services,18.24,
1321,Expense,1/4/2013,1127,0,Consulting,HST - Input tax,2.37,
1321,Expense,1/4/2013,1127,0,,,20.61,20.61
1322,Expense,7/3/2013,1128,0,Utilities,Cash on Hand,,105.5
1322,Expense,7/3/2013,1128,0,Utilities,Utilities,93.36,
1322,Expense,7/3/2013,1128,0,Utilities,HST - Input tax,12.14,
1322,Expense,7/3/2013,1128,0,,,105.50,105.5
1323,Expense,4/3/2013,1129,0,Bell,Cash on Hand,,466.69
1323,Expense,4/3/2013,1129,0,Bell,Telephone,413.00,
1323,Expense,4/3/2013,1129,0,Bell,HST - Input tax,53.69,
1323,Expense,4/3/2013,1129,0,,,466.69,466.69
1324,Expense,1/3/2013,1130,0,Consulting,Cash on Hand,,20.61
1324,Expense,1/3/2013,1130,0,Consulting,IT Services,18.24,
1324,Expense,1/3/2013,1130,0,Consulting,HST - Input tax,2.37,
1324,Expense,1/3/2013,1130,0,,,20.61,20.61
1325,Expense,6/3/2013,1131,0,Utilities,Cash on Hand,,79.09
1325,Expense,6/3/2013,1131,0,Utilities,Utilities,69.99,
1325,Expense,6/3/2013,1131,0,Utilities,HST - Input tax,9.10,
1325,Expense,6/3/2013,1131,0,,,79.09,79.09

目的の出力からわかるように、新しいものが必要です。

  • 次の行が次の行の場合は、前の行の最初、3番目、5番目の列をコピーします。NULL
  • 前の行の列5がNULL印刷されN/Aた場合
  • 2番目の列のすべての項目をExpense(からGeneral Journal)に変更します。
  • 8列(借方)は既存の値です。減らす13%
  • 新しい行の挿入とHST税(13%)の計算

私は今まで何をしましたか?私はStackExchange全体を検索し、次のような結果を得ました。この記事では:)

    awk '{
       split($0,D,/[^[:space:]]*/);
       s = "";
       for(i=1;i<=NF;i++){ 
            if($i~/NoData/){ $i =  last[i]; } 
            last[i]=$i ; 
            s = s  sprintf("%s%s",D[i],$i) 
       }  
       print s
 }' file

ベストアンサー1

このawkスクリプト努力する説明する内容を実装してください。

BEGIN   { OFS = FS = "," }

NR == 1 { name = "N/A" }

NR > 1 {
    # use values from previous row if missing
    if ($1 == "") $1 = trans
    if ($3 == "") $3 = date
    if ($5 == "") $5 = name

    $2 = "Expense"

    $9 = 0.13 * $8
    $8 -= $9

    # set values that may be used by the next row
    trans = $1
    date  = $3
    name  = ($5 == "" ? "N/A" : $5)
}

{ print }

提供したサンプルデータに対して次のコマンドを実行します。

$ awk -f script.awk file.csv
Trans #,Type,Date,Num,Name,Memo,Account,Debit,Credit
1321,Expense,1/4/2013,1127,N/A,Consulting,Cash on Hand,0,0
1321,Expense,1/4/2013,,N/A,,IT Services,17.9307,2.6793
1321,Expense,1/4/2013,,N/A,,,17.9307,2.6793
1321,Expense,1/4/2013,,N/A,,,0,0
1322,Expense,7/3/2013,1128,N/A,Utilities,Cash on Hand,0,0
1322,Expense,7/3/2013,,N/A,,Utilities,91.785,13.715
1322,Expense,7/3/2013,,N/A,,,91.785,13.715
1322,Expense,7/3/2013,,N/A,,,0,0
1323,Expense,4/3/2013,1129,N/A,Bell,Cash on Hand,0,0
1323,Expense,4/3/2013,,N/A,,Telephone,406.02,60.6697
1323,Expense,4/3/2013,,N/A,,,406.02,60.6697
1323,Expense,4/3/2013,,N/A,,,0,0
1324,Expense,1/3/2013,1130,N/A,Consulting,Cash on Hand,0,0
1324,Expense,1/3/2013,,N/A,,IT Services,17.9307,2.6793
1324,Expense,1/3/2013,,N/A,,,17.9307,2.6793
1324,Expense,1/3/2013,,N/A,,,0,0
1325,Expense,6/3/2013,1131,N/A,Utilities,Cash on Hand,0,0
1325,Expense,6/3/2013,,N/A,,Utilities,68.8083,10.2817
1325,Expense,6/3/2013,,N/A,,,68.8083,10.2817
1325,Expense,6/3/2013,,N/A,,,0,0

これは予想出力と一致しませんが、予想出力が最初から仕様に準拠していないため、あまり説明しませんでした。

おすすめ記事