1

We have a file that arrives daily and has a sequence number in the trailer like

9999  TR  SAP _AP Distribution  0000000000000001

where 0000000000000001 is the sequence number and the file that comes the next day should have 0000000000000002 and so on. We need to Validate Sequence Number in input file, reject the file if Sequence Number is duplicate or not matching with expected sequence number. Need a script that does the validation.

The file name is like EFEGECC_Timestamp.txt, e.g. EFEGECC_20150704.txt and so on. We have a file coming in everyday of the year.

  • What are names of your files to be validated, and how many do you have? Is that information with a serial number at the last line of file? – yaegashi Jul 5 '15 at 2:25
  • the file name is like EFEGECC_Timestamp.txt ex EFEGECC_20150704.txt and so on. we have a file coming in everyday of the year – user86683 Jul 5 '15 at 3:14
  • Use file for this. Your description almost already looks like a -magic file. – mikeserv Jul 5 '15 at 4:52
  • If a day is missing, should the corresponding sequence number be skipped? E.g. if EFEGECC_20150101.txt is present, EFEGECC_20150102.txt is missing and EFEGECC_20150103.txt is present, should the sequence number in EFEGECC_20150103.txt be 2 or 3? – Gilles Jul 5 '15 at 12:50
  • the sequence number should still be 2 in EFEGECC_20150103.txt – user86683 Jul 5 '15 at 14:42
1

Assuming you have all files to be validated from the beginning.

#!/bin/bash

seqno=1
for i; do
        if tail -1 $i | grep -q $(printf "%016d\$" $seqno); then
                echo "  valid: $i"
                seqno=$((seqno+1))
        else
                echo "invalid: $i"
        fi
done

Input:

$ grep . EFEGECC*
EFEGECC_20150101.txt:9999  TR  SAP _AP Distribution  0000000000000001
EFEGECC_20150102.txt:9999  TR  SAP _AP Distribution  0000000000000002
EFEGECC_20150103.txt:9999  TR  SAP _AP Distribution  0000000000000002
EFEGECC_20150104.txt:9999  TR  SAP _AP Distribution  0000000000000009
EFEGECC_20150105.txt:9999  TR  SAP _AP Distribution  0000000000000003

Output:

$ ./script.sh EFEGECC*
  valid: EFEGECC_20150101.txt
  valid: EFEGECC_20150102.txt
invalid: EFEGECC_20150103.txt
invalid: EFEGECC_20150104.txt
  valid: EFEGECC_20150105.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.