㈠ 为什么我使用的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 閫掑綊鎼滅储