1833012 Members
2949 Online
110048 Solutions
New Discussion

Re: Find, grep and amend

 
SOLVED
Go to solution
Declan Heerey
Frequent Advisor

Find, grep and amend

I have several directories containing several files. What i want to go is recursively grep a string from these files and manipulate it.

I.e. i want to grep for all instances of CREATE TABLE chop the different
and create a grants file i.e.

GRANT SELECT ON
to
GRANT SELECT ON
to
.......

Any quick ways to achieve this?

Thanks in advance

Declan
3 REPLIES 3
TwoProc
Honored Contributor
Solution

Re: Find, grep and amend

Something like this... ?
Put this in a file, then make it executable, then run it.
Need to change the names of the directories for the find command.
Need to change the name of SOMEUSER.

#!/bin/ksh
find /dir1 /dir2 /dir2 -type f -name "*.sql" -exec grep -i "CREATE TABLE" {} \; > /var/tmp/mylist.$$
cat /var/tmp/mylist.$$ | awk '{ print "GRANT SELECT ON " $3 " TO SOMEUSER " }' > grant_list.sql
rm /var/tmp/mylist.$$
We are the people our parents warned us about --Jimmy Buffett
RAC_1
Honored Contributor

Re: Find, grep and amend

Goto the dir, you want to search.
Does this grep line has fixed position for word?? (Auusming 3 rd position on the line)

find . -type f -print | while read LINE
grep -q "CREATE TABLE" ${LINE}
if [[ $? -eq 0 ]];then
grep "CREATE TABLE" $LINE |awk '{print "GRANT SELECT ON", $3, "to", "user"}' >> /tmp/somefile 2>/dev/null
done

Anil
There is no substitute to HARDWORK
Declan Heerey
Frequent Advisor

Re: Find, grep and amend

Cheers, both work a treat.