I have a csv file with 20 fields. I want to have a script to check if the file is valid according with the following points:
ex:
f1|f2|f3|...|f20
1|aaaa|Y|...|2014/06/25
2|bb|Y...|2014/06/25
3|ccc|N...|2014/06/25
regex:
f1 [0-9]
f2 [a-z]{2,4}
f3 [YN]
.
.
.
f20 [1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]
What are the best shell tools to do it? Do you have any similar script?
Best tool in Unix systems is awk for this job. You can use an awk command like this:
awk 'BEGIN{FS=OFS="|"} NF!=20{print "not enough fields"; exit}
!($1~/^[0-9]$/) {print "1st field invalid"; exit}' file.csv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With