Monday, November 28, 2011

Shell Code to Recursively search a directory for files and process them one by one

#!/bin/sh
$FILENAME="RECORDS"
for FILE in `find . -name $FILENAME`
do
## Process $FILE
echo "FILE: $FILE"
cat "$FILE" |
(
# Read Header but don't process it
read line
# Read records
while read line
do
echo $line
done
)
done


The above shell script code searches through current directory (and subdirectories) for all files with name RECORDS. It then reads the files one by one and processes them line by line.
Eg. If your record file is like follows
"Name","TelNo"
"A","1"
"B","2"
...
Then you could put following code to read off all names in while loop.
$NAME=`echo $line| cut -d"," -f1`
echo $NAME

No comments:

Post a Comment