Re: [問題] script 字串比對

看板Linux作者時間16年前 (2009/10/09 11:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《septemhill.bbs@ptt.cc (冰淇淋乾杯)》之銘言: : 想請問各位一下 : 我有個變數中放一個字串 : 然後我想判斷這個字串裡面有沒有包涵我要的部份字串 : 如果有,則接著往下做 : 目前我是卡在不知道要如何做判斷這個部份 : 以下是我目前所想到的部份: : #!/bin/bash : for file in $(ls $1*) : do : tmpstring=$(file $file) : if [ $tmpstring ] #目前if內的條件式不知道該如何做 : then : ...... : else : ...... : fi : done : 我目前想做的,就是判斷tmpstring中是否有我要的部份字串 : 有的話,則做then,否則做else : 所以還請各位先進指點一下,謝謝 假設有一個純文字檔叫做abc,txt,內容如下: ++++++++++++++++++++++++++++++++++++++ this is a book. the speech is so suck. He is Vencent and gifted on painting. she loves boys who paints well. of course, this is a painting book. +++++++++++++++++++++++++++++++++++++++ 我們取出包含"paint"字串的句子出來,其餘不管,並另存到一個檔案去: ======================================== #!/bin/bash ## script "grepString.sh" [ -e output.txt ] && rm output.txt stringA="*paint*" while read line do echo "$line" | egrep "$stringA" | tee -a output.txt done < $1 exit 0 ========================================= 你可以使用sh grepString.sh abc.txt 應該可以達到你要得效果。 -- 我不是 但我想飛 -- 未來最舊小棧 Oldest Future Object █▇█˙通訊頻率 OfO.twbbs.org ◢▉◤ ◆來源座標 140.109.80.232
文章代碼(AID): #1ApgTrP1 (Linux)
文章代碼(AID): #1ApgTrP1 (Linux)