find grep的問題

看板Linux作者 (飛天豬)時間6年前 (2019/10/04 09:09), 6年前編輯推噓5(5018)
留言23則, 6人參與, 6年前最新討論串1/1
find /aaa/ -name "bbb.file" -exec grep "ccc" {} \ ; 請問收尋不到ccc該如何印出"no match"呢? google過知道grep有回傳值但不知道該如何抓 謝謝大大 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.136.209.240 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1570151393.A.2C6.html ※ 編輯: xup6ru8u4 (101.136.209.240 臺灣), 10/04/2019 09:11:30

10/04 12:28, 6年前 , 1F
-exec <grep片語> \; | tee 1>&2 | [ `wc -c`='0' ] &&
10/04 12:28, 1F

10/04 12:28, 6年前 , 2F
echo No Match
10/04 12:28, 2F

10/04 12:28, 6年前 , 3F
以上省略 -exec之前
10/04 12:28, 3F

10/04 14:06, 6年前 , 4F
發現還是不對,請忽略
10/04 14:06, 4F
還是謝謝你抽空回覆

10/04 15:44, 6年前 , 5F
grep "ccc" `find /aaa/ -name "bbb.file"`
10/04 15:44, 5F

10/04 15:54, 6年前 , 6F
grep -lv 可列出搜不到的檔案
10/04 15:54, 6F
我只要印出no match謝謝 ※ 編輯: xup6ru8u4 (101.136.209.240 臺灣), 10/04/2019 16:13:04 ※ 編輯: xup6ru8u4 (101.136.209.240 臺灣), 10/04/2019 16:13:55 ※ 編輯: xup6ru8u4 (101.136.209.240 臺灣), 10/04/2019 16:15:21

10/04 17:04, 6年前 , 7F
其實 -lv 也不對 XD, -c 再加其他判斷應該就可以
10/04 17:04, 7F
找不到不會顯示0,我也不知道該抓哪個參數 ※ 編輯: xup6ru8u4 (101.136.209.240 臺灣), 10/04/2019 17:28:43

10/04 18:05, 6年前 , 8F
找不到參數就自己刻一個... 把 -exec 換成
10/04 18:05, 8F

10/04 18:06, 6年前 , 9F
| xargs grep c; if [ $? -eq 1 ]; then echo "xx";fi
10/04 18:06, 9F

10/04 18:06, 6年前 , 10F
推文字數限制... 把上面的 xx 換成 no match
10/04 18:06, 10F

10/04 18:08, 6年前 , 11F
xagrs 的參數再自己調一下
10/04 18:08, 11F
謝謝回覆

10/04 22:53, 6年前 , 12F
-exec含後面換掉,改成|xargs grep "ccc" ||echo "字串"
10/04 22:53, 12F

10/04 22:56, 6年前 , 13F
整串變成: find /aaa/ -name "bbb.file" |xargs
10/04 22:56, 13F

10/04 22:56, 6年前 , 14F
grep "ccc" || echo "no match"
10/04 22:56, 14F

10/04 22:58, 6年前 , 15F
find /aaa/ -name "bbb.file" -exec grep "ccc" {} \;||
10/04 22:58, 15F

10/04 22:58, 6年前 , 16F
echo "no match"
10/04 22:58, 16F
謝謝回覆,沒找到確實會印出no match 但有找到的話會印出 bbb no match

10/04 22:59, 6年前 , 17F
自己接一下
10/04 22:59, 17F

10/05 20:40, 6年前 , 18F
find /aaa/ -name "bbb.file" \( -exec grep "ccc" {}
10/05 20:40, 18F

10/05 20:40, 6年前 , 19F
\; -o -exec echo No match \; \)
10/05 20:40, 19F
用小括號似乎有問題

10/05 21:26, 6年前 , 20F
樓上正解, -exec 其實是也判斷式
10/05 21:26, 20F
※ 編輯: xup6ru8u4 (101.137.56.237 臺灣), 10/07/2019 09:49:18 ※ 編輯: xup6ru8u4 (101.137.56.237 臺灣), 10/07/2019 09:53:41 ※ 編輯: xup6ru8u4 (101.137.56.237 臺灣), 10/07/2019 10:34:44 ※ 編輯: xup6ru8u4 (101.137.26.77 臺灣), 10/07/2019 22:43:34

10/08 11:16, 6年前 , 21F
不要bbb的話最後加上|grep -v "bbb"就好,
10/08 11:16, 21F

10/08 11:17, 6年前 , 22F
我以為你只要no match
10/08 11:17, 22F

10/08 13:47, 6年前 , 23F
中括號可能要跳脫, \{\} 或 '{}'
10/08 13:47, 23F
※ 編輯: xup6ru8u4 (101.137.34.1 臺灣), 10/09/2019 00:44:49
文章代碼(AID): #1TbflXB6 (Linux)
文章代碼(AID): #1TbflXB6 (Linux)