0

Daily we receive multiple files (i.e, 0839_ls2_cdr.dat, 0839_ls2_con.dat, 0839_ls2_ddr.dat , etc). Each file contains a previous working day date in second row , first column.

I want to validate all these files contains previous business working day or not before loading them in database.

If any file does not contains previous date and received should through message. As "incorrect date file".

Date format is : yyyymmdd

Could someone please help me out to solve this using shell script.

jay
  • 11
  • The OP essentially wants the required ca. 3 line long shellscript for the task. His question is not unclear, it is pretty clear, it is another thing if it is ontopic or not. I vote for "leave open". – peterh Nov 14 '17 at 06:20

2 Answers2

1

Simpler approach (shown with a check for the current date):

for f in [0-9][0-9][0-9][0-9]_ls2_???.dat; do
  sed '1d;2q' "$f" | grep -q "^$(date +%Y%m%d)" &&
    load into database "$f"
done

(Original answer)

One possible approach:

find /path/to/ingest/directory -type f -name '????_ls2_???.dat' \
  -exec awk -v prev_bus_day="$(command that prints previous business day as yyyymmdd)" \
  -v default_exit=1 '
    NR == 2 {
      if ($1 == prev_bus_day) {
        default_exit = 0
      }
      exit
    }
    END {
      exit default_exit
    }
  ' {} \; -exec command to load multiple files into database {} +

For further reading about using find with other commands, see:

Wildcard
  • 36,499
-1
$ cat l.txt
10 101117
20171113 30
#!/bin/bash
i=date +%Y%m%d -d "1 days ago"

awk 'NR == "2" '{print $0}' l.txt. | awk -v i="$i" '{if ($1 == i) {print "validate"}else{print "invalid date"}}
grg
  • 197