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
# 22  
Old 07-20-2006
threads merged, please do not start multiple threads for the same topic.
# 23  
Old 07-20-2006
Reborg,

I am sorry for that!!! I got no answer so thought that new thread may incerase people viewing.

Anyway can you help me with this please?
# 24  
Old 07-20-2006
Actually that exactly the reason that it is againts the rules of these forums.

However try this:

Code:
#!/bin/ksh
dir=/biddf/ab6498/dev/ctl
export dir
set -x 
myfilepattern=$@
integer filecount=0
for file in $myfilepattern_???_$(date +%Y%m%d) ; do
filecount=$filecount+1
done

if (( $filecount == 10 )); then
print "success"
exit 0
else
print "failure"
exit 1
fi

# 25  
Old 07-20-2006
Reborg,

I bow to you completely. You are great. It's working great man. Smilie

I will learn from this thread a lot. I will be active member from now on.

Once again thank you very much.
# 26  
Old 07-24-2006
Hi,

I have a problem with the script below. Basically it checks whether there is any file present with the name CARE01_DLY present for today's date in the directory /biddf/ab6498/dev/ctl and if the count =1 it says success otherwise error. But it doesn't seem to take the todays date file but checking if the file is present and saying success if the file is present with out checking for todays date. But i want the script to check for todays file only and if found it should through success otherwise error. The name of the file is sent as an argument to the script.

Quote:
#!/bin/ksh
dir=/biddf/ab6498/dev/ctl
export dir
set -x
myfilepattern=$@
integer filecount=0
for file in $myfilepattern_???_$(date +%Y%m%d) ; do
filecount=$filecount+1
done

if (( $filecount == 10 )); then
print "success"
exit 0
else
print "failure"
exit 1
fi
here is the output: The script is called like this script3.ksh BARE02_DLY

Quote:
+ dir=/biddf/ab6498/dev/ctl
+ export dir
+ set -x
+ myfilepattern=BARE02_DLY
+ typeset -i filecount=0
+ date +%Y%m%d
+ filecount=0+1
+ let 1 == 1
+ print success
success
+ exit 0
There is no file with this name for today but it's saying success.

Please help.
# 27  
Old 07-24-2006
I found two problem in your script

$myfilepattern_???_$(date +%Y%m%d) is interpreted by the shell as ${myfilepattern_}???_$(date +%Y%m%d).
You must code like this : ${myfilepattern}_???_$(date +%Y%m%d)


The for statement returns the pattern itself if no file are found.
You must test for that pattern.

Code:
#!/bin/ksh
dir=/biddf/ab6498/dev/ctl
export dir
set -x 
myfilepattern=$@
fullpattern=${my_filepattern}_???_$(date +%Y%m%d)
integer filecount=0
for file in $fullpattern ; do
   [ "$file" != "$fullpattern"] && filecount=$filecount+1
done

if (( $filecount == 10 )); then
print "success"
exit 0
else
print "failure"
exit 1
fi

Jean-Pierre.
# 28  
Old 07-24-2006
Jean,

Thanks for the changes. But the script is still producing wrong result. There are 2 files with todays date in the directory and still it says failure. I think the main problem is pattern matching is is complaring like this:

CARE01_DLY_AUS_20060724 != CARE01_DLY_???_20060724 and saying no match. So please suggest.

Please see output:

Quote:
+ dev=/biddf/ab6498/dev/ctl
+ cd /biddf/ab6498/dev/ctl
+ echo /biddf/ab6498/dev/ctl
/biddf/ab6498/dev/ctl
+ myfilepattern=CARE01_DLY
+ + date +%Y%m%d
fullpattern=CARE01_DLY_???_20060724
+ typeset -i filecount=0
+ [ CARE01_DLY_AUS_20060724 != CARE01_DLY_???_20060724]
./script4.ksh[11]: test: ] missing
+ [ CARE01_DLY_MKT_20060724 != CARE01_DLY_???_20060724]
./script4.ksh[11]: test: ] missing
+ let 0 == 1
+ print failure
failure
+ exit 1
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