Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to validate csv fields

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:

  • It needs to have 20 fields separated by pipes.
  • Each of the 20 fields should match a regex.
  • Know the line and field number for any regex unmatch.

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?

like image 300
odew Avatar asked Oct 21 '25 06:10

odew


1 Answers

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
like image 59
anubhava Avatar answered Oct 23 '25 21:10

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!