如何改進我的 shell 程式效率 - Linux
By Margaret
at 2010-08-29T13:44
at 2010-08-29T13:44
Table of Contents
各位好, 我寫了一支 shell script, 目的在搜尋現行目錄所有 ".txt" 檔內的字串.
若 ".txt" 檔有我要的字串, 就把該字串的整行輸出到 result.txt. 因為我要搜尋字
串有很多, 就先把字串都寫入 strings.txt, 再用迴圈一個個的搜尋.
##################### 以下是我的 shell script ################################
string=`cat strings.txt`
for file_name in `ls -F | grep ".txt"`
do
if [ $file_name != "strings.txt" ] && [ $file_name != "result.txt" ]
then
for name in $string
do
grep -w $name $file_name >> result.txt
done
fi
done
##################### 以上是我的 shell script ################################
但我後來發現, 這樣的寫法很沒有效率. 假設共有 5 個字串在 strings.txt 中,
則根據以上 script 的邏輯, 每個檔案都要被我搜尋 5 次去一一的找出每一個字串.
請問各位, 我該如何去改進這一點? 讓我的程式更有效率.
--
Tags:
Linux
All Comments
By Todd Johnson
at 2010-08-31T16:03
at 2010-08-31T16:03
By Jack
at 2010-09-02T01:10
at 2010-09-02T01:10
By Frederica
at 2010-09-02T21:05
at 2010-09-02T21:05
By Emily
at 2010-09-03T16:45
at 2010-09-03T16:45
By Eartha
at 2010-09-07T08:26
at 2010-09-07T08:26
By Ingrid
at 2010-09-10T15:25
at 2010-09-10T15:25
By Mia
at 2010-09-14T17:02
at 2010-09-14T17:02
By Heather
at 2010-09-15T04:11
at 2010-09-15T04:11
By Wallis
at 2010-09-17T13:49
at 2010-09-17T13:49
By Christine
at 2010-09-21T23:17
at 2010-09-21T23:17
By Carol
at 2010-09-25T12:19
at 2010-09-25T12:19
By Ivy
at 2010-09-27T04:36
at 2010-09-27T04:36
By Agnes
at 2010-09-30T02:52
at 2010-09-30T02:52
By Susan
at 2010-10-03T04:10
at 2010-10-03T04:10
By Hedda
at 2010-10-04T09:46
at 2010-10-04T09:46
By Enid
at 2010-10-07T15:02
at 2010-10-07T15:02
By Rae
at 2010-10-10T07:50
at 2010-10-10T07:50
By Lily
at 2010-10-14T23:11
at 2010-10-14T23:11
By Skylar DavisLinda
at 2010-10-16T20:59
at 2010-10-16T20:59
Related Posts
星海爭霸連線
By Ursula
at 2010-08-28T23:55
at 2010-08-28T23:55
請問wubi
By Heather
at 2010-08-28T23:22
at 2010-08-28T23:22
更新至10.04之後的mount error
By Enid
at 2010-08-28T22:38
at 2010-08-28T22:38
Ubuntu 如何改用舊的kernel開機
By Frederic
at 2010-08-28T15:38
at 2010-08-28T15:38
更新至10.04之後的mount error
By Queena
at 2010-08-28T11:53
at 2010-08-28T11:53