×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùÔÚLinuxÖÐʹÓÃawkÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦Àí £¿

ÔõÑùÔÚlinuxÖÐʹÓÃawkÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦Àí £¿

¼ò½é:

ÔÚLinuxϵͳÖУ¬awkÊÇÒ»ÖÖÇ¿Ê¢µÄÎÄÌìÖ°ÎöºÍ´¦Àí¹¤¾ß¡£Ëü¿ÉÒÔÓÃÀ´ËÑË÷ºÍÌáÈ¡ÎļþÖеÄÌض¨ÄÚÈÝ£¬¾ÙÐÐÊý¾ÝÃûÌû¯£¬ÒÔ¼°Ö´ÐÐһЩ¼òÆÓµ«ÓÐÓõÄÅÌËã¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxÖÐʹÓÃawkÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦Àí£¬°üÀ¨³£ÓõÄawkÏÂÁîÑ¡ÏîºÍʾÀý´úÂë¡£

Ò»¡¢awkÏÂÁîµÄ»ù±¾Óï·¨ºÍÑ¡Ïî

awkÏÂÁîµÄ»ù±¾Óï·¨ÈçÏ£º

awk 'pattern { action }' file

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬patternÊÇÓÃÀ´Æ¥ÅäÎļþÖеÄÎı¾µÄģʽ£¬¿ÉÒÔÊÇÒ»¸öÕýÔò±í´ïʽ»òÕßÒ»¸ö×Ö·û´®£»actionÊÇÔÚÆ¥Åäµ½µÄÎı¾ÐÐÉÏÒªÖ´ÐеIJÙ×÷£¬¿ÉÒÔÊÇÒ»¸öÏÂÁî»òһϵÁÐÏÂÁfileÊÇÒª¾ÙÐд¦ÀíµÄÎļþÃû¡£ÈôÊDz»Ö¸¶¨ÎļþÃû£¬ÔòawkÏÂÁîĬÈ϶ÁÈ¡±ê×¼ÊäÈë¡£

³£ÓõÄawkÏÂÁîÑ¡ÏîÈçÏ£º

-F£ºÖ¸¶¨ÊäÈëÎļþµÄ×Ö¶ÎÍÑÀë·û£¬Ä¬ÒÔΪʹÓÿոñ×÷Ϊ×Ö¶ÎÍÑÀë·û¡£

-v var=value£ºÉèÖÃÒ»¸öawk±äÁ¿µÄÖµ¡£

-f scriptfile£º´ÓÖ¸¶¨µÄ¾ç±¾ÎļþÖжÁÈ¡awkÏÂÁî¡£

¶þ¡¢Ê¾Àý£ºÆÊÎöApache»á¼ûÈÕÖ¾

¼ÙÉèÎÒÃÇÓÐÒ»¸öApache»á¼ûÈÕÖ¾Îļþ£¬ÆäÃûÌÃÈçÏ£º

192.168.0.1 - - [10/Nov/2022:10:30:00 +0800] "GET /index.html HTTP/1.1" 200 1234
192.168.0.2 - - [10/Nov/2022:10:31:00 +0800] "GET /about.html HTTP/1.1" 404 5678
...

µÇ¼ºó¸´ÖÆ

ÎÒÃÇÏ£ÍûʹÓÃawkÏÂÁîÆÊÎö¸ÃÈÕÖ¾Îļþ£¬Í³¼Æÿ¸öÒ³Ãæ±»»á¼ûµÄ´ÎÊýºÍ×ÜÁ÷Á¿¡£ÒÔÏÂÊÇÒ»¸öʾÀýawkÏÂÁ

awk -F'[ "]+' '{count[$7]++; total[$7]+=$NF} END {for (page in count) print page, count[page], total[page]}' access.log

µÇ¼ºó¸´ÖÆ

Ú¹ÊÍ£º

-F'[ “]+’£ºÖ¸¶¨×Ö¶ÎÍÑÀë·ûΪһ¸ö»ò¶à¸ö¿Õ¸ñºÍË«ÒýºÅ¡£

count[$7]++£ºÊ¹ÓÃÊý×écount¼Í¼ÿ¸öÒ³Ãæ±»»á¼ûµÄ´ÎÊý£¬ÒÔÒ³ÃæURLΪË÷Òý¡£

total[$7]+=$NF£ºÊ¹ÓÃÊý×étotal¼Í¼ÿ¸öÒ³ÃæµÄ×ÜÁ÷Á¿£¬ÒÔÒ³ÃæURLΪË÷Òý¡£$NFÌåÏÖ×îºóÒ»¸ö×ֶΣ¬¼´Á÷Á¿×ֶΡ£

END£ºÔÚ´¦ÀíÍêËùÓÐÐкóÖ´ÐеIJÙ×÷¡£

for (page in count) print page, count[page], total[page]£º±éÀúcountÊý×飬²¢´òӡÿ¸öÒ³ÃæµÄURL¡¢»á¼û´ÎÊýºÍ×ÜÁ÷Á¿¡£

Èý¡¢Ê¾Àý£º¹ýÂ˺ÍÐÞ¸ÄÎļþÄÚÈÝ

¼ÙÉèÎÒÃÇÓÐÒ»¸öÎı¾Îļþ£¬°üÀ¨Ò»Ð©ÎÞЧµÄIPµØµã¡£ÎÒÃÇÏ£ÍûʹÓÃawkÏÂÁî¹ýÂ˵ôÕâЩÎÞЧµÄIPµØµã£¬²¢½«ÎļþÖеÄIPµØµãÐÞ¸ÄΪ”xxx.xxx.xxx.xxx”µÄÐÎʽ¡£ÒÔÏÂÊÇÒ»¸öʾÀýawkÏÂÁ

awk -v invalidIP="192.168.0.1|127.0.0.1" '{if ($0 ~ /^([0-9]{1,3}.){3}[0-9]{1,3}$/ && $0 !~ invalidIP) {gsub(/[0-9]{1,3}(.[0-9]{1,3}){3}/, "xxx.xxx.xxx.xxx")} print}' file

µÇ¼ºó¸´ÖÆ

Ú¹ÊÍ£º

-v invalidIP=”192.168.0.1|127.0.0.1″£ºÉèÖÃÒ»¸öawk±äÁ¿invalidIPµÄֵΪÎÞЧµÄIPµØµã£¬Ê¹ÓÔ|”ÍÑÀë¶à¸öIPµØµã¡£

$0 ~ /^([0-9]{1,3}.){3}[0-9]{1,3}$/ && $0 !~ invalidIP£ºÊ¹ÓÃÕýÔò±í´ïʽƥÅäIPµØµã£¬²¢É¨³ýÎÞЧµÄIPµØµã¡£

gsub(/[0-9]{1,3}(.[0-9]{1,3}){3}/, “xxx.xxx.xxx.xxx”)£ºÊ¹ÓÃgsubº¯ÊýÌ滻ƥÅäµ½µÄIPµØµãΪ”xxx.xxx.xxx.xxx”¡£

print£º´òÓ¡¹ýÂ˺ÍÐ޸ĺóµÄÎı¾ÐС£

×ܽá:

±¾ÎÄÏÈÈÝÁËÔõÑùÔÚLinuxϵͳÖÐʹÓÃawkÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦Àí¡£Í¨¹ýʾÀý´úÂ룬ÎÒÃÇѧϰÁËawkÏÂÁîµÄ»ù±¾Óï·¨ºÍ³£ÓÃÑ¡Ï²¢ÑÝʾÁËÔõÑùͳ¼ÆÈÕÖ¾ÎļþÖÐÿ¸öÒ³ÃæµÄ»á¼û´ÎÊýºÍ×ÜÁ÷Á¿£¬ÒÔ¼°ÔõÑù¹ýÂ˺ÍÐÞ¸ÄÎļþÄÚÈÝ¡£Ê¹ÓÃawkÏÂÁî¿ÉÒÔ×ÊÖúÎÒÃǸü¸ßЧµØÆÊÎöºÍ´¦ÀíÎı¾Êý¾Ý£¬ÔÚÈÕÖ¾ÆÊÎö¡¢Êý¾ÝÃûÌû¯µÈ³¡¾°ÖÐÓÐ×ÅÆÕ±éµÄÓ¦Óá£

ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÖÐʹÓÃawkÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦Àí £¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ