LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   a script required to check field value from a file records. (https://www.linuxquestions.org/questions/linux-networking-3/a-script-required-to-check-field-value-from-a-file-records-4175628798/)

vivekn1980 05-01-2018 02:56 PM

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

syg00 05-01-2018 05:45 PM

Given the tags you assigned, I'd leave out bash.
So, what efforts have you made yourself ?.

vivekn1980 05-01-2018 06:52 PM

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

syg00 05-02-2018 12:15 AM

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 ...

vivekn1980 05-03-2018 12:25 PM

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.

vivekn1980 05-03-2018 05:20 PM

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.