㈠ 為什麼我使用的linux不支持grep -E
|root@localhost:~/xly/02# cat ggg/kkk.php
<?php
define('VERSION','2.0.0');
define('SIGN','laofuzi');
?>
root@localhost:~/xly/02# grep -E "V|S" ggg/kkk.php
define('VERSION','2.0.0');
define('SIGN','laofuzi');
root@localhost:~/xly/02# grep -E "^ " ggg/kkk.php
define('VERSION','2.0.0');
define('SIGN','laofuzi');
root@localhost:~/xly/02# grep -E "^<" ggg/kkk.php
<?php
root@localhost:~/xly/02# egrep "^<" ggg/kkk.php
<?php
我的專都可以屬= =!
㈡ linux grep -d skip/skip/recurse 是讀目錄文件採取讀或者跳過,我感覺這個沒有用啊,用了和沒用一樣!
個人理解,舉例子,過濾關鍵字'DEVICE=ens33',過濾位置/etc/* ,就是在/etc/目錄下過濾出關鍵字'DEVICE=ens33'的行
#read讀取
[root@localhost~]#grep-dread'DEVICE-ens33'/etc/*
會發現得到很多:grep: /etc/xxx: Is a directory,我給的范圍是/etc/*,grep過濾字元時肯定要打開文件讀取到數據吧,而我給的路徑有的目錄,目錄怎麼讀取,所以讀目錄的時候會報錯,那麼我認為read是默認的模式,並且不會過濾子目錄內的文件!
#recurse遞歸
[root@localhost~]#grep-drecurse'DEVICE=ens33'/etc/*
得到結果:/etc/sysconfig/network-scripts/ifcfg-ens33:DEVICE=ens33發現,過濾的路徑不再是/etc/xxx,而是將/etc/所有的目錄包括子目錄都在過濾范圍內了,而沒有提示xxx Is a directory,可能是讀取子目錄時發現是目錄,就進到子目錄在進行過濾 [0_0]異想天開,反正,這不就是遞歸了嘛。
#skip跳過
[root@localhost~]#grep-dskip'DEVICE=ens33'/etc/*
#發現什麼都沒有,而我使用
[root@localhost~]#grep-dskip'/root:/bin/bash'/etc/*
#結果如下
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd-:root:x:0:0:root:/root:/bin/bash
發現了當我使用skip這個選項,只有/etc/xxx這級目錄能被過濾出來,發現它並沒有遞歸了,並且沒有提示xxx Is a directory。從字面意思上看,skip,貌似是目錄的話就跳過了。。。
㈢ linux的grep命令錯誤
cat [${i}].log | grep '' |cut -c 54-68
這句為什麼grep的內容是一個空字元串呢?
其他替代語句:
awk-F='/Output=/{print$2}'${i}.log
過濾出含有Output=的行,根據=分割並列印第二個欄位(即=後的欄位)。
㈣ linux 鏂囨湰澶勭悊宸ュ叿涔嬩竴grep鍛戒護璇﹁В
grep(Globel Search Regular Expression and Printing out the line)鍏ㄩ潰鎼滅儲姝e垯琛ㄨ揪寮忓苟鎶婅屾墦鍗板嚭鏉ワ級鏄涓縐嶅己澶х殑鏂囨湰鎼滅儲宸ュ叿錛屾槸涓涓瀵硅岃繘琛屾搷浣滅殑鎼滅儲宸ヤ綔錛屽畠鑳戒嬌鐢ㄦe垯琛ㄨ揪寮忔悳緔㈡枃鏈錛屽苟鎶婂尮閰嶇殑琛屾墦鍗板嚭鏉ャ俇nix鐨刧rep瀹舵棌鍖呮嫭grep銆乪grep鍜宖grep銆 egrep琛ㄧず鎵╁睍鐨刧rep錛岀浉姣攇rep鏀鎸佹洿澶氱殑鍏冨瓧絎︼紝"grep -E"鐩稿綋浜巈grep銆俧grep鏄痜ast grep錛屼笉鏀鎸佸厓瀛楃︼紝浣嗘槸鎼滅儲閫熷害鏇村揩銆倈rep鎼滅儲鐨勭粨鏋滆閫佸埌灞忓箷錛屼笉褰卞搷鍘熸枃浠跺唴瀹廣
1銆乬rep鐨勮娉昜] (man grep鏌ョ湅grep鐨勫府鍔╂枃妗)
grep [options] 'pattern' FILE
鍛戒護 閫夐」 妯″紡 鏂囦歡
grep涓嶅姞寮曞彿鐩存帴榪囨護瀛楃︿覆;grep鍦ㄨ繘琛屾ā寮忓尮閰嶇殑鏃跺欏繀欏誨姞寮曞彿錛屽崟寮曞拰鍙屽紩鍙烽兘鍙浠;grep鍦ㄥ紩鐢ㄥ彉閲忕殑鏃跺欏繀欏誨姞鍙屽紩鍙
2銆乬rep鐨勯夐」[option]
-r錛氶掑綊鐨勬悳緔
-v:鍙嶅悜閫夊彇,鍙鏄劇ず涓嶇﹀悎妯″紡鐨勮
-o:鍙鏄劇ず琚妯″紡鍖歸厤鍒扮殑瀛楃︿覆錛岃屼笉鏄鏁翠釜琛
-i:鍖歸厤鏃朵笉鍖哄垎澶у皬鍐
-A #:鏄劇ず鍖歸厤鍒扮殑琛屾椂錛岄『渚挎樉紺哄悗闈㈢殑#琛(#琛ㄧず鏁板)
-B #:鍓嶉潰鐨#琛
-C #:鍓嶅悗鐨#琛
-E:浣跨敤鎵╁睍鐨勬e垯琛ㄨ揪寮
eg:grep閫夐」鐨勪緥瀛
澶嶅埗浠g爜
浠g爜濡備笅:
cat eg1.text EOF
This is first
how are you
How old are you
fine,thanks
what,so what
What is your name
EOF
grep "you" eg1.text
grep -o "you" eg1.text
grep -v "you" eg1.text
grep -i "what" eg1.text
grep -A 1 "fine" eg1.text
grep -B 1 "fine" eg1.text
grep -C 1 "fine" eg1.text
浠ヤ笂浠g爜鐩存帴綺樿創澶嶅埗鍦╨inux涓婂彲鐩存帴榪愯岋紝浠g爜瑙i噴榪愯屾晥鏋滐紝濡備笅
3銆 姝e垯琛ㄨ揪寮(man regex)鏄鎸囦竴涓鐢ㄦ潵鎻忚堪鎴栬呭尮閰嶄竴緋誨垪絎﹀悎鏌愪釜鍙ユ硶瑙勫垯鐨勫瓧絎︿覆鐨勫崟涓瀛楃︿覆錛岄氬父琚鐢ㄦ潵媯緔㈡垨鏇挎崲閭d簺絎﹀悎鏌愪釜
妯″紡鐨勬枃鏈鍐呭廣傛e垯琛ㄨ揪寮忓垎涓猴細鍩烘湰姝e垯琛ㄨ揪寮忓拰鎵╁睍姝e垯琛ㄨ揪寮忋
鍏冨瓧絎﹀氨鏄鎸囬偅浜涘湪姝e垯琛ㄨ揪寮忎腑鍏鋒湁鐗規畩鎰忎箟鐨勪笓鐢ㄥ瓧絎︺
grep鏀鎸佸熀鏈姝e垯琛ㄨ揪寮忕殑鍏冨瓧絎︼細
^:閿氱偣琛岄栫殑絎﹀悎鏉′歡鐨勫唴瀹癸紝鐢ㄦ硶鏍煎紡"^pattern"
$:閿氱偣琛岄栫殑絎﹀悎鏉′歡鐨勫唴瀹癸紝鐢ㄦ硶鏍煎紡"pattern$"
^$:鍖歸厤絀虹櫧琛
.:鍖歸厤浠繪剰鍗曚釜瀛楃
*:鍖歸厤緔ф尐鍦ㄥ墠闈㈢殑瀛楃︿換鎰忔(0,1,澶氭)
.*:鍖歸厤浠繪剰闀垮害鐨勪換鎰忓瓧絎
/?:鍖歸厤緔ф尐鍦ㄥ墠闈㈢殑瀛楃0嬈℃垨1嬈
/{m,n/}:鍖歸厤鍏跺墠闈㈢殑瀛楃﹁嚦灝憁嬈★紝鑷沖歯嬈
/{m,/}:鍖歸厤鍏跺墠闈㈢殑瀛楃﹁嚦灝憁嬈
/{m/}:綺劇『鍖歸厤鍓嶉潰鐨刴嬈
/{0,n/}:0鍒皀嬈
/:閿氱偣璇嶉----鐩稿綋浜/b,鐢ㄦ硶鏍煎紡錛/pattern
/:閿氱偣璇嶅熬,鐢ㄦ硶鏍煎紡:/pattern
/pattern/錛氬崟璇嶉敋鐐
/(/):鍒嗙粍錛岀敤娉曟牸寮忥細/(pattern/),寮曠敤絎涓涓灝忔嫭鍙風殑鍒嗙粍/1,絎浜屼釜鏄/2,浠ユょ被鎺
[]錛氬尮閰嶆寚瀹氳寖鍥村唴鐨勪換鎰忓崟涓瀛楃
[^]錛氬尮閰嶆寚瀹氳寖鍥村栫殑浠繪剰鍗曚釜瀛楃
eg:鍩烘湰姝e垯琛ㄨ揪鐨勪緥瀛
(1)銆佹樉紺/proc/meminfo鏂囦歡涓浠ヤ笉鍖哄垎澶у皬鐨剆寮澶寸殑琛;
grep "^[sS]" /proc/meminfo
(2)銆佹樉紺/etc/passwd涓浠nologin緇撳熬鐨勮;
grep "nologin$" /etc/passwd
(3)銆佹樉紺/etc/inittab涓絀烘牸寮澶寸殑琛;
grep "^$" /etc/inittab
(4)銆佹樉紺/etc/passwd涓錛屼互r寮澶寸殑瀛楃﹁屽悗璺熶簡浠繪剰鍗曚釜瀛楃︾殑琛;
grep --color "^r." /etc/passwd
(5)銆佹樉紺/etc/passwd涓錛屼互r寮澶村悗璺熶簡o錛宱鍑虹幇浠繪剰嬈$殑琛;
grep --color "^ro*" /etc/passwd
(6)銆佹樉紺/etc/passwd鏂囦歡涓錛宺鍚庤窡浜嗕換鎰忛暱搴︿換鎰忓瓧絎﹀悗璺熶簡h鐨勮;
grep --color "r.*h" /etc/passwd
(7)銆佹樉紺/etc/passwd涓錛宺鍚庤窡浜唎錛宱鍑虹幇0嬈℃垨鑰1嬈$殑琛;
grep --color "ro/?" /etc/passwd
(8)銆佹樉紺/etc/passwd涓錛宺鍚庤窡浜唎錛宱鍑虹幇鑷沖皯1嬈¤嚦澶2嬈$殑琛;
grep --color "ro/{1,2/}" /etc/passwd
(9)銆佹樉紺/etc/passwd涓錛宺鍚庤窡浜唎錛宱鍙鍑虹幇2嬈$殑琛;
grep --color "ro/{2/}" /etc/passwd
(10)銆佹樉紺/etc/passwd涓錛屽尮閰峳oot榪欎釜鍗曡瘝鐨勮;
grep --color "/root/" /etc/passwd
grep鏀鎸佹墿灞曡〃杈懼紡鐨勫厓瀛楃︼細鏀鎸佹墍鏈夊熀鏈姝e垯琛ㄨ揪寮忕殑鍏冨瓧絎︼紝鏈変簺鍜屽熀鏈鍏冨瓧絎﹀湪鐢ㄦ硶涓婁笉涓鏍鳳紝鎵╁睍姝e垯琛ㄨ揪寮忕殑鍛戒護egrep鎴栬単rep -E
?:鍖歸厤緔ф尐鍦ㄥ墠闈㈢殑瀛楃0嬈℃垨1嬈
{m,n}:鑷沖皯m嬈★紝鑷沖歯嬈
():鍒嗙粍
+:鑷沖皯鍖歸厤鍓嶉潰鐨勫瓧絎︿竴嬈
a|b:鍖歸厤a鎴栬卋
eg:鎵╁睍姝e垯琛ㄨ揪寮忕殑渚嬪瓙
(1)銆佹樉紺/etc/passwd涓錛宺寮澶村悗璺熶簡o錛宱鍑虹幇0嬈℃垨鑰1嬈$殑琛岋紱
egrep --color "ro?" /etc/passwd
(2)銆佹樉紺/etc/passwd涓錛宺寮澶村悗璺熶簡o錛宱鍑虹幇鑷沖皯1嬈¤嚦澶2嬈$殑琛岋紱
egrep --color "ro{1,2}" /etc/passwd
(3)銆佹樉紺/etc/inittab鏂囦歡涓浠ヤ竴涓鏁板瓧寮澶村苟浠ヤ竴涓涓庡紑澶存暟瀛楃浉鍚岀殑鏁板瓧緇撳熬鐨勶紱
egrep --color "^([0-9]).*/1$" /etc/inittab
(4)銆佹樉紺/etc/passwd涓錛宺寮澶村悗璺熶簡o錛宱鍑虹幇鑷沖皯1嬈★紱
澶嶅埗浠g爜
浠g爜濡備笅:
egrep --color "ro+" /etc/passwd
grep -E --color "ro{1,}" /etc/passwd
(5)銆佹樉紺/etc/passwd涓錛屽尮閰峳oot鎴栬卙alt鐨勮岋紱
egrep --color "root|halt" /etc/passwd
(6)銆佹樉紺/var/log/secure鏂囦歡涓鍖呭惈"LOGIN ON"鎴栬"Failed passwd"鐨勮岋紱
egrep --color "(LOGIN ON|Failed passwd)" /var/log/secure
grep鏀鎸佸瓧絎﹀拰瀛楃﹂泦鍚
/d:鏁板瓧瀛楃﹀尮閰嶃傜瓑鏁堜簬 [0-9]銆
/s:鍖歸厤浠諱綍絀虹櫧瀛楃︼紝鍖呮嫭絀烘牸銆佸埗琛ㄧ︺佹崲欏電︾瓑銆備笌 [ /f/n/r/t/v] 絳夋晥銆
/S:鍖歸厤浠諱綍闈炵┖鐧藉瓧絎︺備笌 [^ /f/n/r/t/v] 絳夋晥
/w:鍖歸厤浠諱綍瀛楃被瀛楃︼紝鍖呮嫭涓嬪垝綰褲備笌鈥淸A-Za-z0-9_]鈥濈瓑鏁堛
/W:涓庝換浣曢潪鍗曡瘝瀛楃﹀尮閰嶃備笌鈥淸^A-Za-z0-9_]鈥濈瓑鏁堛
[:digit:]:鎵鏈夋暟瀛, 鐩稿綋浜0-9 鎴栬/d
[:lower:]:鎵鏈夌殑灝忓啓瀛楁瘝
[:upper:]:鎵鏈夌殑澶у啓瀛楁瘝
[:alpha:]:鎵鏈夌殑瀛楁瘝
[:alnum:]:鐩稿綋浜嶽0-9a-zA-Z]
[:space:]:絀虹櫧瀛楃 鐩稿綋浜/s
[:punct:]:鎵鏈夋爣鐐圭﹀彿
eg錛氭敮鎸佸瓧絎﹂泦鍚堢殑渚嬪瓙
(1)銆佹樉紺/etc/rc.d/rc.sysinit涓浠#寮澶達紝涓斿悗闈㈣窡涓涓鎴栧氫釜絀虹櫧瀛楃︼紝鑰屽悗鍙堣窡浜嗕換鎰忛潪絀虹櫧瀛楃︾殑琛岋紱
澶嶅埗浠g爜
浠g爜濡備笅:
grep "^#[[:space:]]/{1,/}[^[:space:]]" /etc/rc.d/rc.sysinit
grep -E "^#/s{1,}/S" /etc/rc.d/rc.sysinit
(2)銆佹樉紺/etc/inittab涓鍖呭惈浜:涓涓鏁板瓧:(鍗充袱涓鍐掑彿涓闂翠竴涓鏁板瓧)鐨勮岋紱
澶嶅埗浠g爜
浠g爜濡備笅:
grep --color ":[[:digit:]]:" /etc/inittab
grep --color ":/d:" /etc/inittab
緇忓吀鐨勪緥瀛
(1)銆佸垎緇勭殑渚嬪瓙
澶嶅埗浠g爜
浠g爜濡備笅:
cat test.txt EOF
He like his liker
He love his lover
She love her lover
She like her lover
EOF
grep "l..e.*l..er" test.txt
grep "/(l..e/).*/1r" test.txt
(2)銆佸尮閰1-255鐨勬暟瀛
澶嶅埗浠g爜
浠g爜濡備笅:
cat num.txt EOF
12
234
255
256
EOF
grep --color -E "/([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/" num.txt
(3)銆佸尮閰岮BC綾籌P鍦板潃鍗 1.0.0.1---223.255.255.254
澶嶅埗浠g爜
浠g爜濡備笅:
cat ip.txt EOF
1.0.0.254
1.0.0.255
1.2.3.4
223.255.255.254
224.255.255.252
2.255.255.255
EOF
grep -E --color "/([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])/.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])/" ip.txt
(4)銆佸尮閰岴mail鍦板潃:浠繪剰闀垮害鏁板瓧瀛楁瘝@浠繪剰闀垮害鏁板瓧瀛楁瘝.錛坈om|org|net絳夌瓑錛
澶嶅埗浠g爜
浠g爜濡備笅:
cat email.txt EOF
5678967@qq.com
jerry#[email protected]
EOF
grep -E --color "^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$" email.txt
(5)銆佸尮閰嶆墜鏈哄彿鐮侊細鎵嬫満鍙風爜鏄1[3|4|5|8]鍚庨潰鎺9浣嶆暟瀛楃殑
澶嶅埗浠g爜
浠g爜濡備笅:
cat tel.txt EOF
13690876890
12589098379
15608764083
15820974619
138074082711
18618203761
19209783900
1329873909
EOF
grep --color -E "/1[3|4|5|8][0-9]{9}/" tel.txt
鏈鏂囧嚭鑷 鈥滄妧鏈涔嬭礬---妗鈥 鍗氬
㈤ 在linux 安裝mysql後,為什麼用rpm -qa |grep mysql 卻什麼都沒有顯示
用service mysql start服務啟動成功,說明MYSQL啟動正常,用rpm -qa |grep mysql查看沒有,是操作過程中錯誤造成的。解決方法如下:
1、登錄mysql官網,找到一個自己想要的版本。
㈥ linux grep鍛戒護璇﹁В
grep鍛戒護鏄痩inux涓嬬殑涓涓鏂囨。緙栬緫鍛戒護錛屽畠涓昏佹湁涓涓鍔熻兘錛屽叿浣撲粙緇嶅備笅錛
grep鍛戒護鏄涓涓寮哄ぇ鐨勬枃鏈鎼滅儲宸ュ叿錛岄氬父鍙浠ョ敤鍦ㄥ規悳緔㈣繃紼嬬殑琛ュ厖銆
璇存槑錛歡rep鍛戒護鐨勬ā寮忔槸闈炲父鐏墊椿鐨勶紝鍙浠ユ槸鍙橀噺銆佸瓧絎︿覆絳夛紝鍥犳ゅ湪浣跨敤鏃惰佹敞鎰忔ā寮忎腑鏄鍚﹀寘鍚浜嗙┖鏍礆紝濡傛灉鏈夛紝涓瀹氳佺敤鍙屽紩鍙峰皢鍏跺紩璧鋒潵銆
grep鍛戒護鐨勫叿浣撳懡浠ょ殑璇娉曟牸寮忎負grep [鍙傛暟]銆
鍙傛暟錛
-i 鎼滅儲鏃訛紝蹇界暐澶у皬鍐
-c 鍙杈撳嚭鍖歸厤琛岀殑鏁伴噺
-l 鍙鍒楀嚭絎﹀悎鍖歸厤鐨勬枃浠跺悕錛屼笉鐢ㄥ垪鍑哄叿浣撶殑鍖歸厤琛
-n 鍒楀嚭鎵鏈夌殑鍖歸厤琛岋紝鏄劇ず琛屽彿
-h 鏌ヨ㈠氭枃浠舵椂涓嶆樉紺烘枃浠跺悕
-s 涓嶆樉紺轟笉瀛樺湪銆佹病鏈夊尮閰嶆枃鏈鐨勯敊璇淇℃伅
-v 鏄劇ず涓嶅寘鍚鍖歸厤鏂囨湰鐨勬墍鏈夎
-w 鍖歸厤鏁磋瘝
-x 鍖歸厤鏁磋
-r 閫掑綊鎼滅儲