shenwei356 / csvtk

A cross-platform, efficient and practical CSV/TSV toolkit in Golang
http://bioinf.shenwei.me/csvtk
MIT License
1.01k stars 84 forks source link

Allow cut on non-existent columns #156

Closed tseemann closed 3 years ago

tseemann commented 3 years ago
% csvtk version
csvtk v0.23.

% cat foo.csv
A,B
1,2
3,4

% csvtk cut -f A,C foo.csv

# THIS HAPPENS
[ERRO] column "C" not existed in file: a.csv

# DESIRED OPTION 1
% csvtk cut --allow-missing-col -f A,C foo.csv
A
1
3

# DESIRED OPTION 2
% csvtk cut --blank-missing-col  -f A,C foo.csv
A,C
1,
3,

This would be usefulf or cleaning large datatsets that I need to merge or get partial versions from different sources.

shenwei356 commented 3 years ago

New flags

  -m, --allow-missing-col   allow missing column
  -b, --blank-missing-col   blank missing column

Tests

$ cat foo.csv | csvtk cut -f D,A,C,B -m | csvtk pretty 
A   B
-   -
1   2
3   4

$ cat foo.csv | csvtk cut -f D,A,C,B -m -b | csvtk pretty 
D   A   C   B
-   -   -   -
    1       2
    3       4

$ cat foo.csv | csvtk cut -f 5,2,4,1 -m | csvtk pretty 
B   A
-   -
2   1
4   3

$ cat foo.csv | csvtk cut -f 5,2,4,1 -m -b  
,B,,A
,2,,1
,4,,3

Binaries

tseemann commented 3 years ago

Awesome! Thanks @shenwei356 !