0

consider todays date as 24/02/14

I have set of files in mention directory "/apps_kplus/KplusOpenReport/crs_scripts/rconvysya" and file names as

INTTRADIVBMM20142402
INTTRADIVBFX20142402
INTTRADIVBFI20142402
INTTRADIVBDE20142402
INTPOSIVBIR20142402
INTPOSIVBIR20142302
INTTRADIVBDE20142302
INTTRADIVBFI20142302
INTTRADIVBFX20142302
INTTRADIVBMM20142302

I wanted to get count of file with current date. for that i am using below query

#! /bin/bash
tm=$(date +%y%d%m)
x=$(ls /apps_kplus/KplusOpenReport/crs_scripts/rconvysya/INT*$tm.txt 2>/dev/null | wc -l)
if [ $x -eq 5 ]
then
exit 4
else
exit 3
fi

But not getting desired output. what is wrong.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user3164140
  • 591
  • 2
  • 8
  • 15

4 Answers4

0

You're searching for files with a .txt extension, and the files you list have none. If you remove the .txt from the pathname, it works.

rojomoke
  • 3,765
  • 2
  • 21
  • 30
0

You can try the following awk:

awk -v d="$date" '{split(d,ary,/\//);if($0~"20"ary[3]ary[1]ary[2]) cnt++}END{print cnt}' file

where $date is your shell variable containing the date you wish to search for.

$ cat file
INTTRADIVBMM20142402
INTTRADIVBFX20142402
INTTRADIVBFI20142402
INTTRADIVBDE20142402
INTPOSIVBIR20142402
INTPOSIVBIR20142302
INTTRADIVBDE20142302
INTTRADIVBFI20142302
INTTRADIVBFX20142302
INTTRADIVBMM20142302
$ awk -v d="$date" '{split(d,ary,/\//);if($0~"20"ary[3]ary[1]ary[2]) cnt++}END{print cnt}' file
5
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
0

With Awk, anything is possible!

tm=$(date +%Y%d%m)   # Capital Y is for the four digit year!
ls -a | awk -v tm=$tm  '$0 ~ tm' | wc -l

This is the Great and Awful Awk Command. (Awful as in both full of awe and as in Starbucks Coffee).

The -v parameter allows me to set Awk variables. The $0 represents the entire line which is the file name from the ls command. The $0 ~ tm means I'm looking for files that contain the time string you specified.

I could do this too:

 ls -a | awk "/$tm\$/" 

Which lets the shell interpolate $tm into the Awk program. This is looking only for files that end in your $tm stamp. The /.../ by itself means matching the entire line. It's an even shorter Awk shortcut than I had before. Plus, it makes sure you're only matching the timestamp on the end of the file.

David W.
  • 105,218
  • 39
  • 216
  • 337
0
ls /apps_kplus/KplusOpenReport/crs_scripts/rconvysya | grep "`date +%Y%m%d`"| wc -l

This worked for me and it seems like the simplest solution, I hope it fits you needs

To get exit status 4 if there are 0k files present use code bellow:

0=`ls /apps_kplus/KplusOpenReport/crs_scripts/rconvysya | grep "`date +%Y%m%d`"| xargs -n 1 stat -c%s | grep "^0$" | wc -l`
if [ "$0" -eq "0" ]
then 
exit 4
else 
exit 3
fi
sgp667
  • 1,797
  • 2
  • 20
  • 38
  • Hey thanks this works for me. Now i am able to capture current dated file out of all files. How can i check size of these files means if size of each file is greater than 0k then exit 4 else exit 3 – user3164140 Feb 26 '14 at 06:13
  • So let me get this straight. First you want to find out how many files have today's date in it and second you want to know if ALL of these files are bigger than 0k. – sgp667 Feb 26 '14 at 17:43