2017-09-03 52 views
1

我正在使用Notepad ++的「查找文件」(Ctrl + Shift + F)功能來搜索文本文件中字符串的出現。 我的文本文件(.SQL)位於一個目錄的子文件夾(如「視圖」和「MaterializedViews」下面):記事本++多搜索

C:\users\Schema Export\schema_name\Views 
C:\users\Schema Export\schema_name\ MaterializedViews 

例如,對於「cust_account在「查找全部」點擊「(在目錄」 C:\ Users \用戶模式導出\模式名\「)給了我在‘查找結果’記事本的屏幕:

C:\users\Schema Export\schema_name\Views\SM.sql (2 hits) 
C:\users\Schema Export\schema_name>\Views \V_ACCOUNT.sql (8 hits) 
C:\users\Schema Export\schema_name\Views \V_ACCOUNT_rework.sql (8 hits) 

新搜索‘pos_row_id’來說吧,給我:

C:\users\Schema Export\schema_name\Views\V_ACCOUNT.sql (2 hits) 
C:\users\Schema Export\schema_name\Views\V_ACCOUNT_rework.sql (2 hits) 
C:\users\Schema Export\schema_name\Views\V_HUB_REFER.sql (1 hit) 

在降級模式,我現在在搜索窗口中獲得兩行:

Search "pos_row_id" (5 hits in 3 files) 
Search "cust_account" (22 hits in 4 files) 

我想你完成這項搜索一次性像獲得:

Search value1 (x1 hits in y1 files), where value1 is "pos_row_id" in example above, 
Search value2 (x2 hits in y2 files), where value2 is "cust_account" in example above, 
Search value3 (x3 hits in y3 files), etc. 
Search value4 (x4 hits in y4 files) 
Search value5 (x5 hits in y5 files) 
Search value6 (x6 hits in y6 files) 
(..) 

我的第一個想法是記錄一個宏並更新它(使用我的不同值,值1,值2等複製現有的代碼)
不幸的是,宏代碼不是在「shortcuts.xml」文件中生成的(這個主題上有很多線程...... )

是否有任何其他的可能性(可能與一個python腳本)從一個值列表中檢索所有的搜索結果如上所述?

謝謝!

+0

如果您不確定如何詢問/評論(我看到您嘗試編輯我的帖子以通知我澄清),您應該查看[幫助中心](https://stackoverflow.com/help)。我會更新我的答案,但如果您有任何問題,請在下次發表評論,而不是編輯。 – EsotericVoid

+0

而你可能會得到一個語法錯誤,因爲你沒有運行最後的Python版本(我在我的答案中已經指定)。 – EsotericVoid

回答

0

使用python 3.6.2。我首先將這些值存儲爲字典,然後將打印時找到的匹配相加。

import os 
from collections import defaultdict 


def find_all(*args): 
    matches = defaultdict(dict) 
    for subdir, dirs, files in os.walk('.'): # walk through all files in all subdirectories 
     for file in files: 
      with open(os.path.join(subdir, file)) as f: 
       data = f.read() 
       for arg in args: 
        c = data.count(arg) # count the number of hits in this file 
        if c > 0: # if positive, add it to the matches 
         matches[arg][os.path.join(subdir, file)] = c 

    return matches 


search = find_all('test', 'file') # your search keys here 
for key in search: 
    print(f"Search {key} ({sum(search[key].values())} hits in {len(search[key])} files),") 

'test''file'是我插入的功能進行測試,修改那些您搜尋的項目只是隨機搜索鍵。您也可以將一個迭代函數傳遞給函數,例如list個單詞來搜索。