2013-07-30 33 views
0

對於我來說,在greg中使用-l標誌來獲取文件列表非常常見。我喜歡有辦法立即打開所有找到的文件(使用subl命令,我假設),而不是僅輸出一個列表。Mac:在grep中打開grep中找到的文件

我目前的工作流程:

grep --include=*.php -R -l "tribe_events_event_classes" . 

,輸出:

./plugins/events-calendar-pro/views/day/loop.php 
./plugins/events-calendar-pro/views/map/loop.php 
./plugins/events-calendar-pro/views/photo/loop.php 
./plugins/events-calendar-pro/views/widgets/mini-calendar/list.php 
./plugins/the-events-calendar/lib/tribe-template-factory.class.php 
./plugins/the-events-calendar/public/template-tags/general.php 
./plugins/the-events-calendar/views/list/loop.php 
./plugins/the-events-calendar/views/month/single-event.php 
./themes/roots/tribe-events/day/loop.php 

然後我通常通過一個與複製並粘貼文件中的一個:

subl ./plugins/events-calendar-pro/views/day/loop.php 
subl ./plugins/events-calendar-pro/views/map/loop.php 
etc... 

我知道這裏有做一個更有效的方法。

+0

我做相反的。我從崇高運行搜索。我在當前文件夾(subl。)中打開ST2,然後右鍵單擊樹窗格上的目錄並選擇Find%Replace。你可以給ST2一個文件掩碼查找逗號分隔。 –

回答

3

這樣做,這將是最簡單的方法:

subl `grep --include=*.php -R -l "tribe_events_event_classes" .` 

(注意是反引號)。

或者:

for file in `grep --include=*.php -R -l "tribe_events_event_classes" .`; do 
    subl $file 
done 

編輯

由於反引號不嵌套好,使用命令替換爲更好:

subl $(grep --include=*.php -R -l "tribe_events_event_classes" .) 
0

一個bash腳本可以很好地解決這個問題。下面是一個未經考驗的概念(可能需要一些調整):

#!/bin/bash 

files=() 

while grep --include=*.php -R -l "tribe_events_event_classes" .; do 
    files += ("$REPLY") 
done 

for file in "${files[@]}"; do 
    subl $file 
done