※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):