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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would I grep a search pattern

Status
Not open for further replies.

abovebrd

IS-IT--Management
May 9, 2000
690
0
0
US
I need to remove files from a given directory. These files contain customer names. I have a database that contains a list of certian customers.<br><br>I need to find a way to use the grep command or some other means to take its search input from the contents of my customer database file.<br><br>The database file is very simple.<br><br>Customer1<br>Customer2<br>Customer3<br>Customer4<br><br>The concept would be grep to use the database file for it's input. If the name was present in a file, the file would then get deleted. And this would continue until all customer names where searched.<br><br>How do I use my databse file as my input search pattern for grep ?<br><br>Any thoughts<br> <p>-Danny<br><a href=mailto: > </a><br><a href= > </a><br>
 
Danny,<br><br>Try this:<br>------------------------------ 8&lt; --------------------------------<br><FONT FACE=monospace>#!/bin/ksh<br><br>for CUSTOMER in `cat dbf`<br>do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FILES=`grep -l $CUSTOMER customer/*`<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [ -n &quot;$FILES&quot; ]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rm $FILES<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi<br>done</font><br><br><br>------------------------------ 8&lt; --------------------------------<br>The text between (and including) the back quotes would be replaced with the contents of dbf, with all the carriage returns removed.<br><br>The for loop then searches for files in the customer/ directory for those containing the customer and then removes any if present.&nbsp;&nbsp;grep -l returns just the filename of files containing the string.<br><br>It is potentially dangerous though if you have a customer called Ted and a customer called Teddy, because both may be deleted when only Ted should be.<br><br>Annihilannic.<br>
 
Thanks allot.<br><br>It looks like that is just what I was looking for.<br><br>Question: <br>Do you have any suggestions on where I might find usfull information on scripting. I am having a little trouble with the if, while, then statements.<br><br>&nbsp; <p>-Danny<br><a href=mailto: > </a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top