Bom dia! Eu tenho um arquivo .CSV com 9 colunas. Preciso analisar seu conteúdo em um novo arquivo, realizar alguns pequenos cálculos e criar uma nova linha no processo. Acho que isso é melhor ilustrado mostrando o .CSV existente e a saída desejada:
Conteúdo .CSV existente (9 colunas)
$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
,,,,,,,,
Saída Desejada
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
Como você pode ver na saída desejada, preciso de algumas coisas novas:
- Duplicar 1ª, 3ª e 5ª colunas da linha anterior se a próxima linha for
NULL
- Se a 5ª coluna na linha anterior for
NULL
, imprimaN/A
em vez disso - Altere todas as entradas na 2ª coluna para
Expense
(deGeneral Journal
) - A 8ª coluna (Débito) é o valor existente menos 13%
- Insira uma nova linha e calcule o imposto HST (13%)
O que eu fiz até agora? Pesquisei em todo o StackExchange e encontrei o seguinte ( deste post :)
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