本篇ShengYu 介紹Linux grep 搜尋字串用法與範例,grep 通常用來搭配其它指令來搜尋字串,例如grep 搭配cat 來搜尋檔案裡的特定字串,grep 算是Linux ... ... <看更多>
「grep -e」的推薦目錄:
- 關於grep -e 在 [問題] grep檔案裡的特殊字元字串- 看板Linux - 批踢踢實業坊 的評價
- 關於grep -e 在 Linux grep 搜尋字串用法與範例 - ShengYu Talk 的評價
- 關於grep -e 在 Linux Crash Course - The grep Command - YouTube 的評價
- 關於grep -e 在 grep · GitHub Topics 的評價
- 關於grep -e 在 How put every files result by a grep into a zip file 的評價
- 關於grep -e 在 Grep excluding line that ends in 0, but not 10, 100 etc 的評價
grep -e 在 Linux Crash Course - The grep Command - YouTube 的推薦與評價
![影片讀取中](/images/youtube.png)
The "Linux Crash Course" series (formerly known as the "Linux Essentials" series) tackles important concepts around Linux, one video at a ... ... <看更多>
grep -e 在 grep · GitHub Topics 的推薦與評價
A collection of handy Bash One-Liners and terminal tricks for data processing and Linux system maintenance. linux shell bash terminal system hardware grep data- ... ... <看更多>
grep -e 在 Grep excluding line that ends in 0, but not 10, 100 etc 的推薦與評價
Don't use grep , do it in awk instead: $ awk '$NF!=0' file text1 98432230 text2 123412 text3 10 line6 40000 ... ... line10 20 . ... <看更多>
grep -e 在 [問題] grep檔案裡的特殊字元字串- 看板Linux - 批踢踢實業坊 的推薦與評價
版友好
有個grep特殊字元的問題想請教
Q:
若想在檔案裡 搜尋特定字串如右 ~!@#$%^&*()_+ {}[]|\:";'<>?, /AZ
假設有一個隨機檔案data.txt內容是
AA="XXX223"
BB=a132423davqef1
CC='OOOOOOOO'
DD="~!@#$%^&*()_+ {}[]|\:";'<>?, /AZ"
EE=e12eee eqwf
發現因為特殊字元的關係 ex: * [
下面指令會出現問題
我試過
(雙引號)
cat data.txt | grep "~!@#$%^&*()_+ {}[]|\:";'<>?, /AZ" => fail
(雙引號, 特殊字元前面加\)
cat data.txt | grep "~!@#$%^&\*()_+ {}\[]|\:\";\'<>?, /AZ" => fail
(單引號)
cat data.txt | grep '~!@#$%^&*()_+ {}[]|\:";'<>?, /AZ' => fail
(為了解決單引號內 特殊字元'字元)
cat data.txt | grep $'\'' => 可以找到'
cat data.txt | grep $'~!@#$%^&\*()_+ {}\[]|' => 可以找到
cat data.txt | grep $'~!@#$%^&\*()_+ {}\[]|\' => 多了\, 就fail
目前用的shell是/bin/sh 不是bash
希望有遇過的版友
可以給一些建議
現在仍在嘗試解決中
若一有解答會回報給大家
感謝
-------------------------------------------------------
今天試了板友的提示
環境是
ubuntu 10.04 / busybox v1.01
Ex: ~\!@#$%^&\*()\_+ {}\[\]|\\\:\";'<>?, /AZ`
當用grep ""(雙引號), 搜尋字串包括 ! * [ ` " \ 等字元, 將無法搜尋,
sol: 這些特殊字元前面需要加上\或\\
特別的是字元 " ` 前面加上\\有問題,
字元 \ 前面需要加上兩個\\, ex: \=>\\\ (反斜線的雙重escape)
ex: cat data.txt |grep "~\!@#$%^&\*()_+ {}\[]|\\\:\";'<>?, /AZ"
若用grep ''(單引號), 搜尋字串包括 * [ ' \ 等字元, 將無法搜尋,
sol: 這些特殊字元前面需要加上\, (但不能加上兩個\)
特別的是字元 ' 前面需要加上'"'", ex: ' => '"'"'
ex: cat data.txt |grep '~!@#$%^&\*()_+ {}\[]|\\:";'"'"'<>?, /AZ'
若用grep $'\x27'繼續延伸找結果, 則是busybox不支援該語法
以上是自己測試的結果
謝謝版友幫忙
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.243.103.132
※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1413471770.A.A69.html
... <看更多>