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!

grep in multiple files and different dir.

Status
Not open for further replies.

jalge2

Technical User
Feb 5, 2003
105
0
0
US
Hi all. I'm creating a script that will search for whatever criteria you want to enter and then display the information. The problem is, I'm not sure how to grep for different directories, or different file names. Some of the file names that I'm looking at are in /dso/eis/log/batlog.log.20030701, or hislog.20030701, and there are also older files that are moved to a different directory /dso/eis/log/backup/batlog.log.20030701. My script looks like this:

# /bin/ksh!
echo "\tPlease enter the battery number you wish to search for...\c"
read ans

if [[ $ans != 0 ]]

then
cat /dso/eis/log/batlog.log.* | grep $ans > /dso/eis/log/battsearch.log

awk -F, 'BEGIN { print "LRT DATE TIME OLD BATT NAME DEPT IN/OUT NEW BATT STATUS"
print "--- ---- ---- -------- ---- ---- ------ -------- ------"}
{ printf "%-7s %-8s %-8s %-12s %-16s %-13s %-10s %-12s %-8s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9 }' /dso/eis/log/batt
search.log
else
exit


fi

print ""
print ""
print ""


echo "\tWould you like to print this? (y/n)"
read ans1

if [ $ans1 = "y" -o $ans1 = "Y" ]

then
lp -dps@pr7 /dso/eis/log/battsearch.log

else
exit
fi

Can someone explain? Up where I'm doing the cat on the file is where the problem lies. Thanks.
 
Just from a quick look...I think you need to quote the * in your cat stmnt with a backslash [.\*] to interpret correctly.
As far as grep for different files and directories...depending on how many you got. You could use a FOR loop with the list of file names;
for filename in filename_1 filename_2...filename_n
do
list
done

If you need to compile the list, try something like this:

Folders=ls -l | grep ^d.\*ba | awk '{print $9}'
for Folder in Folders
do
-your processing-
done

this lists only the directories (as an input to the for loop) that start with "ba."
- the ^d locates the lines with first character 'd' (e.g. directory)
- .\* => \ quotes the * so that grep interprets the .* as any number of any characters.

good luck to you,
Duane
 
Okay... that should have been

for Folder in $FOLDERS
...
Duane =/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top