2009-11-12 77 views
1

我試圖通過刪除應用程序未使用的所有過程來清理舊數據庫。使用grep,我已經能夠確定源代碼中沒有發生單個過程。是否有辦法一次完成所有的程序?查找源代碼中未引用的存儲過程

更新:當使用-E「proc1 | proc2」產生與任一模式匹配的所有文件中的所有行的輸出時,這不是非常有用。遺留數據庫有2000多個程序。

我試圖使用-o選項,認爲我可以使用它的輸出作爲原始模式的逆向搜索模式。但是,我發現在使用具有多個模式的-o選項時沒有輸出。

還有其他想法嗎?

更新:經過進一步的實驗,我發現它是防止輸出的-i和-o選項的組合。不幸的是,我需要在這種情況下不區分大小寫的搜索。

+0

什麼版本的'grep',什麼shell(和版本)和什麼分佈?你是否使用'-v'作爲'grep'的選項之一?看到我編輯的答案。 – 2009-11-18 15:27:45

回答

1

開始用你的程序名稱列表。爲了便於重複使用後,對它們進行排序,讓他們小寫,就像這樣:

tr "[:upper:]" "[:lower:]" < list_of_procedures | sort > sorted_list_o_procs 

...現在你有程序名稱的排序列表。聽起來你已經在使用gnu grep了,所以你已經有了-o選項。

fgrep -o -i -f sorted_list_o_procs source1 source2 ... > list_of_used_procs 

請注意使用fgrep:這些不是正則表達式,真的,所以爲什麼要對待它們。希望你也會發現,這奇蹟般地糾正了你的輸出問題;)。現在你有一個醜陋的使用過程列表。讓我們清理它們,就像我們上面的原始列表一樣。

tr "[:upper:]" "[:lower:]" < list_of_used_procs | sort -u > short_list 

現在您已經使用過程的簡短列表。讓我們找到原始列表中不在短列表中的列表。

fgrep -v -f short_list sorted_list_o_procs 

...他們在那裏。

4

將存儲過程的列表以「|」分隔給egrep,

或:

for stored_proc in $stored_procs 
do 
grep $stored_proc $source_file 
done 
4

我不得不做這在過去也是如此。不要忘記任何可能從其他過程調用的過程。 如果您使用的是SQL Server,您可以使用此:

SELECT名字, 文本 FROM sysobjects中 JOIN syscomments中乙 ON A.id = B.id WHERE的xtype = 'P' 和文字LIKE' %<存儲過程的名稱>%」

+0

+ 1用於檢查別處的建議 - 但不要停止與其他procs,但也考慮工作,DTS/SSIS包,報告客戶端等。哎呀,我已經看到舊的代碼連接字符串形成一個存儲過程的名稱(呸)。 :) – Mayo 2009-11-19 14:00:37

2

我得到下你的編輯介紹的情況下輸出:

$ echo "aaaproc1bbb" | grep -Eo 'proc1|proc2' 
proc1 
$ echo $? 
0 
$ echo "aaabbb" | grep -Eo 'proc1|proc2' 
$ echo $? 
1 

退出代碼顯示如果沒有匹配。

您也可能會發現這些選項grep有用(-L可能是特定於GNU的grep):

 
-c, --count 
       Suppress normal output; instead print a count of matching lines 
       for each input file. With the -v, --invert-match option (see 
       below), count non-matching lines. (-c is specified by POSIX.) 

-L, --files-without-match 
       Suppress normal output; instead print the name of each input 
       file from which no output would normally have been printed. The 
       scanning will stop on the first match. 

-l, --files-with-matches 
       Suppress normal output; instead print the name of each input 
       file from which output would normally have been printed. The 
       scanning will stop on the first match. (-l is specified by 
       POSIX.) 

-q, --quiet, --silent 
       Quiet; do not write anything to standard output. Exit 
       immediately with zero status if any match is found, even if an 
       error was detected. Also see the -s or --no-messages option. 
       (-q is specified by POSIX.) 

對不起,你引述man頁面,但有時它可以幫助篩選事情有點。

編輯

對於(不區分大小寫)的文件名列表不包含任何的程序:

grep -EiL 'proc1|proc2' * 

對於包含的任何程序的文件名列表(區分不區分大小寫):

grep -Eil 'proc1|proc2' * 

列出文件並顯示匹配(不區分大小寫):

grep -Eio 'proc1|proc2' * 
+0

謝謝你的例子。我需要再多研究一下,找出爲什麼我沒有得到預期的產出。 – 2009-11-15 00:56:26