Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading the contents of a file

Status
Not open for further replies.

buxtonicer1

Programmer
Sep 20, 2006
82
GB
Level: Beginner.

I'm doing some processing where I search through the contents of a directory and each file in that directory is
searched to see if it contains a key work. If a file passes the test the error count
stays the same but if a file fails the counter is incremented by 1.
In my directory I want to exclude certain files from being processed (outside the scope of what I want to do) but not sure how to do this.
I basically need to read the contents of an exclusion list and tell my script to ignore all those files in
the exclusion list.
I'm working on someone elses script so that is why I'm not really sure how to approach this. Well I'm really new
to shell scripting and UNIX and find the dev environment challenging also. Any help would be
appreciated ?
 
Hi. Sorry haven't much time this evening, but do the files you need to exclude have something in common in the filename for example? Have you considered an 'inclusion' rather than an exclusion?

 
Here are some useful commands/constructs that might help you get started:

# produce a list of all files and sub-directories in and below /path/to/dir
find /path/to/dir -print

# step through a list of only the files in and below /path/to/dir
for FILE in `find /path/to/dir ! -type d -print`
do
<do something with each file in ${FILE}>
done

# check that a file contains a certain character string
grep 'a certain character string' /path/to/dir/filename

# put in variable 'count' that number of times the string occurs
count=`grep -c 'a certain character string' /path/to/dir/filename`

# test for names in a listfile (name= myname, listfile= myfile)
if [[ $(grep -c 'myname' /path/to/dir/myfile) -eq 0 ]]
then
<myname not found in myfile>
else
<myname found in myfile, one or more times>
fi

# increment a counter called 'linecount'
((linecount=linecount + 1))

There is some help in the man pages (man <command>) and a good place to start is man ksh.

I hope that helps.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top