Shell基本學習(5)---三劍客其Grep

1.curl常見用法(非三劍客)

url=http://www.baidu.com

get請求 curl $url

[root@localhost ~]# url=www.baidu.com
[root@localhost ~]# curl ${url}
<!DOCTYPE html>

post請求 curl -d 'xxx' $curl

proxy使用 curl -x 'http"//127.0.0.1:8080' 

2.文件描述符

*輸入文件-標準輸入0

*輸出文件-標準輸出1

*錯誤出文件-標準錯誤2

*curl

使用2>&1 >/tmp/tmp < /tmp/tmp

————————————————————————————————————————

劍客一grep

(1)在文件中查找文本

語法 grep 內容 文件       , 他會把找到相關內容的一整行打印出來

[root@localhost ~]# grep 百度一下,你就知道 baidu.html
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道<

(2)管道里查詢

語法 獲得打印結果命令 | grep 文本

[root@localhost ~]# curl https://blog.csdn.net/jiulanhao | grep 三劍客
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0        <span class="article-type type-1 float-none">原創</span>        Shell基本學習(5)---三劍客              </a> 
100  147k    0  147k    0     0   209k      0 --:--:-- --:--:-- --:--:--  209k

(3)消除調試顯示

語法 xxxx     2>/dev/null    (固定寫法)

[root@localhost ~]# curl https://blog.csdn.net/jiulanhao 2>/dev/null | grep 三劍客       
        <span class="article-type type-1 float-none">原創</span>        Shell基本學習(5)---三劍客              </a> 

(4)grep也是支持正則的(其實本身也是正則讀取來匹配)

[root@localhost ~]# curl https://blog.csdn.net/jiulanhao 2>/dev/null | grep -E "三劍客|腳本"
        <span class="article-type type-1 float-none">原創</span>        Shell基本學習(5)---三劍客              </a> 
        <span class="article-type type-1 float-none">原創</span>        Shell基本學習(4)---腳本              </a> 
<!-- 多條廣告如下腳本只需引入一次 -->
                                        Shell基本學習(4)---腳本                </a>

(5)grep也支持多個過濾

[root@localhost ~]# curl https://blog.csdn.net/jiulanhao 2>/dev/null | grep -E "三劍客|腳本" | grep 廣告
<!-- 多條廣告如下腳本只需引入一次 -->

(6)排除條件 -v

[root@localhost ~]# curl https://blog.csdn.net/jiulanhao 2>/dev/null | grep -E "三劍客|腳本" | grep -v 廣告
        <span class="article-type type-1 float-none">原創</span>        Shell基本學習(5)---三劍客              </a> 
        <span class="article-type type-1 float-none">原創</span>        Shell基本學習(4)---腳本              </a> 
                                        Shell基本學習(4)---腳本                </a>

(7)過濾文本網站的語句(其實就是正則匹配)(-o只輸出符合的字符串)

[root@localhost ~]# curl https://blog.csdn.net/jiulanhao 2>/dev/null | grep -o 'http://[a-zA-Z0-9\\-\\.]*'
http://passport.csdn.net
http://push.zhanzhang.baidu.com
http://www.w3.org
http://www.w3.org
http://www.w3.org
http://www.w3.org
http://www.baidu.com
http://repo.zabbix.com

tips:

* grep:
傳統的 grep 程序, 在沒有參數的情況下, 只輸出符合 RE 字符串之句子. 常見參數如下:
-v: 逆反模示, 只輸出"不含" RE 字符串之句子.
-r: 遞歸模式, 可同時處理所有層級子目錄裏的文件.
-q: 靜默模式, 不輸出任何結果(stderr 除外. 常用以獲取 return value, 符合爲 true, 否則爲 false .)
-i: 忽略大小寫.
-w: 整詞比對, 類似 \<word\> .
-n: 同時輸出行號.
-c: 只輸出符合比對的行數.
-l: 只輸出符合比對的文件名稱.
-o: 只輸出符合 RE 的字符串. (gnu 新版獨有, 不見得所有版本都支持.)
-E: 切換爲 egrep .

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章