Counting files in a directory that match a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting files in a directory that match a pattern
# 8  
Old 07-20-2006
Glenn,

You made a very good point. But here there will be 20 files for BARE01_DLY_MKT_YYYYMMDD alone and 20 files for BARE02_DLY_MKT_20060803 alone and like that with some more names. So that is why I need to pass BARE01_DLY_MKT_YYYYMMDD as the parameter so that it checks whether that named file has 20 files or not and similarly for other file names. Please remember that MKT is the only that changes in the file. So please help.
# 9  
Old 07-20-2006
It's really the same concept... just pass the entire pattern instead of just the date.

check20.sh:

Code:
#!/bin/ksh
myfilepattern=$1
integer filecount=0
for file in $myfilepattern; do
  filecount=$filecount+1
done

if (( $filecount == 20 )); then
  exit 0
else
  exit 1
fi

Syntax:

check20.sh BARE01_DLY_???_20060803
check20.sh BARE02_DLY_???_20060803
check20.sh BARE01_DLY_???_20060720
etc.
# 10  
Old 07-20-2006
Glenn,

I tried this and the code is not returning error if the file count doesn't match the required count. Please suggest.
# 11  
Old 07-20-2006
Woops! Use $@ instead of $1.

Code:
#!/bin/ksh
myfilepattern=$@
integer filecount=0
for file in $myfilepattern; do
  filecount=$filecount+1
done

if (( $filecount == 20 )); then
  exit 0
else
  exit 1
fi

# 12  
Old 07-20-2006
Glenn,

I ran the code and i am using set -x to debug the code and surprised by the way code is taking. I have created one file named BARE01_DLY_MKT_20060803 in the directory /biddf/ab6498/dev/ctl direcotry and the script should show an error but please see below for the debug statments from the script;

script2.ksh BARE01_DLY_MKT_20060803
+ myfilepattern=BARE01_DLY_MKT_20060803
+ typeset -i filecount=0
+ filecount=0+1
+ let 1 == 10
+ exit 1

It is taking as 1=10 and exit ing from the code with out througing an error. Below is the script I modified.

#!/bin/ksh
dir=/biddf/ab6498/dev/ctl
export dir
set -x
myfilepattern=$@
integer filecount=0
for file in $myfilepattern; do
filecount=$filecount+1
done

if (( $filecount == 10 )); then
exit 0
else
exit 1
fi

Please suugest
# 13  
Old 07-20-2006
Glenn,

I don't need to pass the whole name of the file but just BARE01_DLY would suffice. But it has to check whether 20 files are present for that day or not?


Please suggest.
# 14  
Old 07-20-2006
The "let 1 == 10" line is not setting 1 equal to 10. It is comparing 1 (the number of files it counted) and 10 (the number of files needed for success). Because 1 != 10, the script exits status 1, which is an error. The unix convention is that "0" is success, and anything else is failure.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern match and replace indirect directory reference using sed

Hi, I need a ksh script to replace indirect directory references in an .ini file with a env variable using sed or awk. The .ini file is for example as such: A=.. B=../ C=../.. D=../../ E=../bin F=../../bin G=../../bin/xml H=../../bin/xml/ Need to replace an instance of .. or... (2 Replies)
Discussion started by: andyatit
2 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

counting lines that match pattern

I have a file of 1.3 millions lines. some are with the same word twice on the line, some line have two diffrent words. each line has two words, one in brackets. example: foo (foo) bar (bar) thae (awvd) beladf (vswvw) I am sure this can be done with one line of... (6 Replies)
Discussion started by: robsonde
6 Replies

4. Shell Programming and Scripting

Counting the number of files within a directory input by the user

So I have a loop that stated if a directory exists or not. If it does it prints the number of files within that directory. I use this code... result=`(ls -l . | egrep -c '^-')` However, no matter which directory I input, it outputs the number "2" What is wrong here? (4 Replies)
Discussion started by: itech4814
4 Replies

5. Shell Programming and Scripting

counting the number of characters in the filename of all files in a directory?

I am trying to display the output of ls and also print the number of characters in EVERY file name. This is what I have so far: #!/bin/sh for x in `ls`; do echo The number of characters in x | wc -m done Any help appreciated (1 Reply)
Discussion started by: LinuxNubBrah
1 Replies

6. UNIX for Advanced & Expert Users

Counting files in a given directory

Hi all, Need some help counting files... :) I'm trying to count the number of files in a given directory (and subdirectories) which reportedly contains "thousands" of files. I'm using this: ls -R | wc -l However it's been an hour and looks like it's still running; there is no output... (18 Replies)
Discussion started by: verdepollo
18 Replies

7. Shell Programming and Scripting

pattern search for multiple log files and counting

I have 10 appservers and each appserver has 4 jvms . Each of these logs is archived and stored on a nfs directory . For example the files are /logs/200907/ap1-jvm1.server.log.20090715.gz /logs/200907/ap2-jvm2.server.log.20090714.gz /logs/200908/ap1-jvm1.server.log.20090812.gz I want to... (3 Replies)
Discussion started by: gubbu
3 Replies

8. UNIX for Dummies Questions & Answers

copying a pattern of files in one directory into other with new pattern names...

Hi, I have to copy a set of files abc* in /path/ to /path1/ as abc*_bkp. The list of files appear as follows in /path/: abc1 xyszd abc2 re2345 abcx .. . abcxyz I have to copy them (abc* files only) into /path1/ as: abc1_bkp abc2_bkp abcx_bkp .. . (6 Replies)
Discussion started by: new_learner
6 Replies

9. UNIX for Dummies Questions & Answers

Counting number of files in a directory

Some simple questions from a simple man. If i wanted to count the number of files contained within a directory, say /tmp would ls -l /tmp ¦ wc -l suffice and will it be accurate? second one: How would i check the number of files with a certain string in the filename, in the same directory. ... (2 Replies)
Discussion started by: iamalex
2 Replies

10. Shell Programming and Scripting

rm files in a directory, looping, counting, then exit

I am trying to write a script that will look for a file in a directory, then remove it. I need it to loop until it has removed a certain number of files. Is it better to do a repeat or to list each file in a pattern? Files will be numbered like RAF.01.*, RAF.02.*, etc. Thanks, James (6 Replies)
Discussion started by: JporterFDX
6 Replies
Login or Register to Ask a Question