3.10 mchkcsv - Check CSV Data

Automatically repair (standardize the number of fields for all rows) comma-separated-values (CSV) data that do not meet the f[sect:csv]CSV format for MCMD. Use the -diag option to perform checking on CSV data.

Format

mchkcsv [a=] [-diag] [-r] [i=] [o=] [-nfn] [-nfno] [--help] [--version]

Parameters

i=

Input file name

 

The CSV data defined here is checked for incomplete lines.

 

Issues identified in this file is automatically repaired.

 

If this parameter is omitted, the standard input is used.

a=

Substitute the field name(s) in the input data with the field name(s) specified at this parameter.

 

If the number of fields specified is less than the number of fields in the header,

 

the output will only yield portion of columns from the input data starting from the left.

 

Conversely, if the number of fields specified here is greater than the number of fields in input data,

 

data items in the extra fields are expressed as NULL values in the output.

-diag

Execute check.

 

When this option is specified, write result to standard output.

-r

Exclude control characters

 

This defines the control character as ASCII character code 0x00-0x1f,0x7f (remove 0x09,0x0a,0x0d).

 

Control code is automatically converted to the string &#x when this option is not specified.

Examples

Example 1: Repair data

This data contains different number of columns in all records. For instance, only 3 records have 4 columns. Use M-Command to repair and standardize 3rd and 5th rows to 4 columns.

$ more dat1.csv
product,date,quantity,amount
A,20081201,1,10
A,20081202,2,
A,*,3
B,20081201,4,40
B,20081203,50
$ mchkcsv i=dat1.csv o=rsl1.csv
#END# kgchkcsv i=dat1.csv o=rsl1.csv
$ more rsl1.csv
product,date,quantity,amount
A,20081201,1,10
A,20081202,2,
A,*,3,
B,20081201,4,40
B,20081203,50,

Example 2: Change field name after repairing the data

This data contains different number of columns in all records. For instance, only 3 records have 4 columns. Use M Command to repair and standardize 3rd and 5th rows to 4 columns. At the same time, label the field names from the input data as ¥verb|“product,date,quantity,amount”| starting from the left.

$ more dat2.csv
fld1,fld2,fld3,fld4
A,20081201,1,10
A,20081202,2,
A,*,3
B,20081201,4,40
B,20081203,50
$ mchkcsv a=product,date,quantity,amount i=dat2.csv o=rsl2.csv
#END# kgchkcsv a=product,date,quantity,amount i=dat2.csv o=rsl2.csv
$ more rsl2.csv
product,date,quantity,amount
A,20081201,1,10
A,20081202,2,
A,*,3,
B,20081201,4,40
B,20081203,50,

Example 3: Check data integrity and output diagnostic results

It merely checks for incomplete data structure in the CSV data, and save the diagnosis result in CSV file.

$ mchkcsv -diag i=dat1.csv o=rsl3.csv
#END# kgchkcsv -diag i=dat1.csv o=rsl3.csv
$ more rsl3.csv
#===================================================
# CSVファイル診断 
# file name : dat1.csv
#---------------------------------------------------
# 結果の一文字目の意味
# # : 情報行(問題なし)
# ? : KGMODにて扱えない問題点(?の後の文字は解説参照)
# よって、左端が全て#になればOK
#===================================================
############################ ヘッダー情報(1行目) ###
# 項目数 : 4
# 項目No.  項目名
# 1   product
# 2   date
# 3   quantity
# 4   amount
#
############## EOL(End Of Line)情報(ヘッダー含む) ##
#         LF改行行数 : 6 (LineNo: 0 1 2 ... )
#
################# データ行情報(ヘッダー含まない) ###
#           総行数 : 5
#       総バイト数 : 66
#           平均長 : 13.2
#           最大長 : 16 (LineNo:2)
#           最小長 : 6 (LineNo:4)
# 注:長さは、改行文字も含めた長さ
#
################################# 項目数の一貫性 ###
?g 異なる項目数の行が発見されました。
?g  項目数:3 (LineNo:4)
?g  項目数:3 (LineNo:6)
#
####################################### 項目情報 ###
#  項目番号[1] 項目名[product]
#    NULL値の行数                : 0 
#    DQUOTEで囲われていない行数  : 5 (LineNo: 1 2 3 ... )
#    DQUOTEで囲われている行数    : 0 
#
#  項目番号[2] 項目名[date]
#    NULL値の行数                : 0 
#    DQUOTEで囲われていない行数  : 5 (LineNo: 1 2 3 ... )
#    DQUOTEで囲われている行数    : 0 
#
#  項目番号[3] 項目名[quantity]
#    NULL値の行数                : 0 
#    DQUOTEで囲われていない行数  : 5 (LineNo: 1 2 3 ... )
#    DQUOTEで囲われている行数    : 0 
#
#  項目番号[4] 項目名[amount]
#    NULL値の行数                : 1 (LineNo: 2 )
#    DQUOTEで囲われていない行数  : 3 (LineNo: 1 2 4 )
#    DQUOTEで囲われている行数    : 0 
#
################################### 問題点の解説 ###
# ?a : 同じ項目名があると項目番号を特定できない。
#  【対処方法】kgchkcsv a=x,y,z のように項目名を新たに指定する。
# ?b : 項目名に不正な文字があるとエラーになる
#  【対処方法】kgchkcsv a=x,y,z のように項目名を新たに指定する。
# ?c : KGMODが扱う改行は高速化のためLF(UNIX改行)のみ。
#      この問題はRFC4180には準拠しておらずKGMOD独自の制約である。
#  【対処方法】kgchkcsv にて全てLFに変換される。
#
# ?d : 最終行にLFやCRなどの改行(EOL)文字が存在しない。
#      これはRFC4180にも準拠していない。
#  【対処方法】kgchkcsv にてLFが付加される。
#
# ?e : データファイル内に'\0'が入り込んでいる。
#      テキストファイルでない可能性が高い。
#      RFC4180には準拠していない。
#  【対処方法】kgchkcsv にて、"&#x00
#              kgchkcsv -rにて\0は削除される。
#
# ?f : KGMODが扱える一行の最大長を超過している。
#      現在の設定では1024000バイト以上の長さの行は扱えない。
#  【対処方法】環境変数を設定することで最大値を変更可能である。
#        ex) export KG_MaxRecLen=204800
#      ただし10240000バイトを越えては指定できない。 
#      この問題はRFC4180には準拠しておりKGMOD独自の制約である。
#
# ?g : KGMODでは全行同じ項目数を前提とする。
#      この問題はRFC4180には準拠しておりKGMOD独自の制約である。
#  【対処方法】
#       1) kgchkcsv  データHEADERの項目数に合わせる.
#            超過項目は捨てられ、足りない項目はnull値となる
#       2) kgchkcsv  a=x,y,z HEADER行をスキップし、
#            指定したx,y,zを項目名として1)と同様の処理を行う。
#
# ?h : 制御文字(0x01~0x1F,0x7F)が項目値として入り込んでいる。
#      テキストファイルでない可能性が高い。
#      RFC4180には準拠していない。
#  【対処方法】kgchkcsv にて、"&#x01
#              kgchkcsv -rにて制御文字は削除される。
#
# ?i : TABは利用できない。
#      RFC4180には準拠していない。
#  【対処方法】kgchksv にて、"&#x09
#              kgchkcsv -rにてTABは削除される。
#
# ?j : DQUOTEで囲われていない中でDQUOTEが見つかった
#        ex) NG: xxx,oo"oo,xxx  -> OK: xxx,"oo""oo",xxx
#      RFC4180には準拠していない。
#  【対処方法】kgchkcsv にて上記の変換を行う。
#
# ?k : DQUOTEで囲われている中で単一のDQUOTEが見つかった
#      ex) NG: xxx,"oo"oo",xxx  -> OK: xxx,"oo""oo",xxx
#      RFC4180には準拠していない。
#  【対処方法】kgchkcsvにて上記の変換を行う。
#-------------------------------------------------------------

Related Command