linuxÖÐgrepµÄÓ÷¨ÊÇɶ
linuxÖÐgrepµÄÓ÷¨ÊÇÓÃÀ´ËÑË÷Æ¥ÅäÌض¨Ä£Ê½µÄÎı¾ÐУ¬²¢Êä³öÆ¥ÅäµÄÐС£grepÏÂÁî¾ßÓжàÖÖÑ¡ÏîºÍÓ÷¨£¬¿ÉÒÔƾ֤²î±ðµÄÐèÇó¾ÙÐÐÎÞаµÄËÑË÷ºÍÆ¥Åä²Ù×÷¡£³£ÓÃÑ¡ÏîÓУº1¡¢-i£¬ËÑË÷ʱºöÂÔ×Ö·ûµÄ¾Þϸд£»2¡¢-n£¬ÏÔʾƥÅäµÄÐкţ»3¡¢-c£¬Í³¼ÆÆ¥ÅäµÄÐÐÊý£»4¡¢-r£¬µÝ¹éµØÔÚÖ¸¶¨µÄĿ¼ÏÂËÑË÷Îļþ£»5¡¢-l£¬½öÏÔʾ°üÀ¨Æ¥ÅäÏîµÄÎļþÃû£»6¡¢-v£¬·´ÏòÆ¥Å䣬Êä³ö²»°üÀ¨Æ¥ÅäÏîµÄÐеȵȡ£
±¾½Ì³Ì²Ù×÷ϵͳ£ºlinux6.4.3ϵͳ¡¢Dell G3µçÄÔ¡£
grepÏÂÁîÊÇÔÚLinux/UnixÇéÐÎÖг£ÓõÄÎı¾ËÑË÷¹¤¾ß£¬ËüʹÓÃÕýÔò±í´ïʽÀ´ËÑË÷Æ¥ÅäÌض¨Ä£Ê½µÄÎı¾ÐУ¬²¢Êä³öÆ¥ÅäµÄÐС£grepÏÂÁî¾ßÓжàÖÖÑ¡ÏîºÍÓ÷¨£¬¿ÉÒÔƾ֤²î±ðµÄÐèÇó¾ÙÐÐÎÞаµÄËÑË÷ºÍÆ¥Åä²Ù×÷¡£
»ù±¾Ó÷¨
grepÏÂÁîµÄ»ù±¾Ó÷¨ÈçÏ£º
grep [options] pattern [file...]
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬optionsÊÇgrepÏÂÁîµÄÑ¡ÏÓÃÓÚÖ¸¶¨ËÑË÷ÐÐΪ£»patternÊÇËÑË÷µÄģʽ£¬¿ÉÒÔʹÓÃÕýÔò±í´ïʽÀ´Æ¥ÅäÎı¾£»fileÊÇÎļþÃû£¬Ö¸¶¨ÒªÔÚÄÄЩÎļþÖоÙÐÐËÑË÷¡£
³£ÓÃÑ¡Ïî
ÒÔÏÂÊÇgrepÏÂÁîµÄһЩ³£ÓÃÑ¡Ï
-i£ººöÂÔ¾Þϸд£¬ËÑË÷ʱºöÂÔ×Ö·ûµÄ¾Þϸд¡£
-n£ºÏÔʾƥÅäµÄÐкš£
-c£ºÍ³¼ÆÆ¥ÅäµÄÐÐÊý¡£
-r£ºµÝ¹éµØÔÚÖ¸¶¨µÄĿ¼ÏÂËÑË÷Îļþ¡£
-l£º½öÏÔʾ°üÀ¨Æ¥ÅäÏîµÄÎļþÃû¡£
-v£º·´ÏòÆ¥Å䣬Êä³ö²»°üÀ¨Æ¥ÅäÏîµÄÐС£
-A num£ºÊä³öÆ¥ÅäÏîÖ®ºóµÄÄÚÈÝ£¬numÌåÏÖÒªÊä³öµÄÐÐÊý¡£
-B num£ºÊä³öÆ¥ÅäÏî֮ǰµÄÄÚÈÝ£¬numÌåÏÖÒªÊä³öµÄÐÐÊý¡£
-C num£ºÊä³öÆ¥ÅäÏî֮ǰºÍÖ®ºóµÄÄÚÈÝ£¬numÌåÏÖÒªÊä³öµÄÐÐÊý¡£
ʾÀýÓ÷¨
ÒÔÏÂÊÇһЩgrepÏÂÁîµÄʾÀýÓ÷¨£º
ÔÚÄ¿½ñĿ¼ÏµÄËùÓÐÎļþÖÐËÑË÷°üÀ¨”grep”Òªº¦×ÖµÄÐУº
grep grep *
µÇ¼ºó¸´ÖÆ
ÔÚÎļþÖÐËÑË÷°üÀ¨Ìض¨×Ö·û´®µÄÐв¢Êä³öÐкţº
grep -n 'string' file.txt
µÇ¼ºó¸´ÖÆ
µÝ¹éµØÔÚĿ¼ÏÂËÑË÷ÎļþÖеÄÒªº¦×Ö£º
grep -r 'pattern' /path/to/directory
µÇ¼ºó¸´ÖÆ
·´ÏòÆ¥Å䣬Êä³ö²»°üÀ¨Òªº¦×ÖµÄÐУº
grep -v 'pattern' file.txt
µÇ¼ºó¸´ÖÆ
ËÑË÷°üÀ¨Òªº¦×Öµ«ÔÚÌض¨ÎļþÖв»°üÀ¨Òªº¦×ÖµÄÐУº
grep 'pattern' file1.txt | grep -v -f file2.txt
µÇ¼ºó¸´ÖÆ
ÕâÀïÊÇÒ»¸ö¸üÖØ´óµÄÀý×Ó£¬ÎÒÃǽ«ÔÚÒ»¸öĿ¼¼°Æä×ÓĿ¼ÏµÄËùÓÐPythonÎļþÖÐËÑË÷°üÀ¨”def”Òªº¦×ÖµÄÐУ¬²¢Êä³öÐкźÍÐÐÄÚÈÝ£º
grep -n 'def' /path/to/directory/*.py
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ»¹¿ÉÒÔʹÓõݹéËÑË÷£¬ÀýÈçÔÚÄ¿½ñµÄĿ¼¼°Æä×ÓĿ¼ÏµÄËùÓÐÎļþÖÐËÑË÷°üÀ¨”import”Òªº¦×ÖµÄÐУº
grep -r 'import' ./*
µÇ¼ºó¸´ÖÆ
±ðµÄ£¬ÎÒÃÇ»¹¿ÉÒÔÍŽáÆäËûÏÂÁîʹÓÃgrep£¬ÀýÈçʹÓùܵÀ½«Êä³öת´ï¸øÆäËûÏÂÁî¾ÙÐд¦Àí¡£ÒÔÏÂÊÇÒ»¸öʾÀý£¬ËüʹÓÃgrepÔÚÎļþÖÐËÑË÷°üÀ¨”error”Òªº¦×ÖµÄÐУ¬²¢½«Ð§¹ûת´ï¸ølessÏÂÁîÒÔ·ÖÒ³Éó²é£º
grep 'error' file.txt | less
µÇ¼ºó¸´ÖÆ
ÕâЩʾÀýÖ»ÊÇgrepÏÂÁîµÄһС²¿·Ö¹¦Ð§£¬grepÏÂÁ֧³Ö¸üÖØ´óµÄÕýÔò±í´ïʽºÍ¸ü¶àµÄÑ¡Ïî¡£Äã¿ÉÒÔͨ¹ýÔËÐÐman grepÏÂÁîÀ´Éó²é¸üÏêϸµÄgrepÏÂÁîÎĵµ£¬ÒÔ»ñÈ¡¸ü¶àµÄÐÅÏ¢ºÍʹÓÃÒªÁì¡£
ÒÔÉϾÍÊÇlinuxÖÐgrepµÄÓ÷¨ÊÇɶµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡