a script required to check field value from a file records.
I need a script to check the records in a file , if any value match transfer the record in error.txt file.
1- If any of the any field value is NULL(nothing in this field) Record1|Record2|Record3|Record4|Record5|DATE1|DATE2 Example: 11111111|22222222|NULL|12|444|27042018|27042018 (Record3 is null) 99999999|88888888|007172FD|NULL|975|02052018|27042018 (Record4 is null) 2- If any of the field value is set as 0, except Record4 Example: Record1|Record2|Record3|Record4|Record5|DATE1|DATE2 Record1 = 0 or ‘00000000’ Record2 = 0 or ‘0000000000’ Record3 = 0 or ‘0000000000’ 11111111111|000080809643|00000000|64|374|02052018|27042018 11111111111|000000000000|45678987|64|374|02052018|27042018 0000000000|000082079914|0071170B|1|308|27042018|27042018 4- If Record5 value is negative 11111111111|000045672345|45678987|64|-222|02052018|27042018 |
Given the tags you assigned, I'd leave out bash.
So, what efforts have you made yourself ?. |
awk command
I tried below given awk command but no success
awk -F\| ' NR > 1 {for (i=1; i<=3; i++) if (!$i) {print; next} if ($4 == "") {print; next} if ($5+0 <= 0) {print; next} for (i=6; i<=7; i++) if (!($i ~ /[0-3][0-9][01][0-9]20[12][0-9]/)) {print; next} } ' file > error.txt |
That's one way of doing it.
If you are not getting what you expect, put some diagnostic (extra) prints in to see value of things you are testing - or not. Diagnostics 101. There are some obvious areas of potential concern - how many records are you passing in ?. The string "NULL" is not null ... |
Check date format
i use following awk command:
awk -F'|' -v OFS='|' ' $6 !~ "^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[12])[0-9]{4}$" || $7 !~ "^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[12])[0-9]{4}$" ' but its not matching the date for given record: Record1|Record2|Record3|Record4|Record5|DATE1|DATE2 108899999|0000222222|00AAAAA|-228|60|29102017|27042018 if DATE1 having 10th month its not matched with awk script. |
Working now
Thanks , i use following awk command to check date format DDMMYYYY, it works:
awk -F'|' -v OFS='|' ' $6 !~ "^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9][0-9])$" || $7 !~ "^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9][0-9])$"' |
All times are GMT -5. The time now is 07:59 PM. |