ÔõÑùÔÚLinuxÖÐʹÓÃsedÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦Àí£¿
ÔõÑùÔÚlinuxÖÐʹÓÃsedÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦Àí£¿
СÐò£º
ÔÚLinuxϵͳÖУ¬ÈÕÖ¾Îļþ¼Í¼ÁËϵͳµÄÔËÐÐÇéÐκÍÔËÐÐÈÕÖ¾£¬¹ØÓÚϵͳÖÎÀíÔ±À´Ëµ£¬¶ÔÈÕÖ¾Îļþ¾ÙÐÐÆÊÎöºÍ´¦ÀíÊǺÜÊÇÖ÷ÒªµÄ¡£ÆäÖУ¬sedÏÂÁîÊÇÒ»ÖÖºÜÊÇÇ¿Ê¢µÄÎı¾´¦Àí¹¤¾ß£¬¿ÉÒÔÔÚLinuxÇéÐÎÖжÔÈÕÖ¾Îļþ¾ÙÐиßЧµÄÆÊÎöºÍ´¦Àí¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃsedÏÂÁî¾ÙÐÐÈÕÖ¾µÄÆÊÎöºÍ´¦Àí£¬²¢ÌṩһЩ³£ÓõÄsedÏÂÁîʾÀý¡£
Ò»¡¢Ê²Ã´ÊÇsedÏÂÁ
sed£¨Stream Editor£©ÊÇÒ»ÖÖÁ÷ʽÎı¾±à¼Æ÷£¬Ö÷ÒªÓÃÓÚ¶ÔÎı¾¾ÙÐÐÁ÷ʽ±à¼¡¢Ìæ»»ºÍת»»¡£ÔÚLinuxϵͳÖУ¬sedÊÇ´¦ÀíÎı¾ÎļþÖÐÄÚÈݵÄÒ»ÖÖÇ¿Ê¢¹¤¾ß£¬¿ÉÒÔµÖ´ïÅúÁ¿Ìæ»»¡¢É¾³ý¡¢Ìí¼ÓµÈÄ¿µÄ¡£
¶þ¡¢sedÏÂÁîµÄ»ù±¾Ê¹Óãº
sedÏÂÁî¿ÉÒÔͨ¹ýÒÔÏ·½·¨Ê¹Óãº
sed [option] 'command' file
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬optionΪsedÏÂÁîµÄÑ¡ÏcommandΪҪִÐеÄsedÏÂÁfileΪҪ´¦ÀíµÄÎļþÃû¡£
Èý¡¢³£ÓõÄsedÏÂÁ
Ìæ»»ÏÂÁîs£º
Ìæ»»ÏÂÁîsÊÇsedÏÂÁîÖÐ×î³£ÓõÄÏÂÁÓÃÓÚÔÚÎļþÖвéÕÒ²¢Ìæ»»Ö¸¶¨µÄÎı¾¡£
sed 's/old_text/new_text/g' file
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬old_textΪҪ²éÕÒµÄÎı¾£¬new_textΪҪÌæ»»µÄÐÂÎı¾£¬gÌåÏÖÈ«¾ÖÌæ»»¡£
ɾ³ýÏÂÁîd£º
ɾ³ýÏÂÁîd¿ÉÒÔɾ³ýÖ¸¶¨µÄÐС£
sed 'nd' file
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬nΪҪɾ³ýµÄÐкš£
²åÈëÏÂÁîi£º
²åÈëÏÂÁîi¿ÉÒÔÔÚÖ¸¶¨µÄÐÐÇ°²åÈëеÄÎı¾¡£
sed 'ni ew_text' file
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬nΪҪ²åÈëµÄÐкţ¬new_textΪҪ²åÈëµÄÐÂÎı¾¡£
×·¼ÓÏÂÁîa£º
×·¼ÓÏÂÁîa¿ÉÒÔÔÚÖ¸¶¨µÄÐкó×·¼ÓеÄÎı¾¡£
sed 'na ew_text' file
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬nΪҪ׷¼ÓµÄÐкţ¬new_textΪҪ׷¼ÓµÄÐÂÎı¾¡£
Êä³öÏÂÁîp£º
Êä³öÏÂÁîp¿ÉÒÔÏÔʾָ¶¨ÐлòÖ¸¶¨Îı¾¡£
sed -n 'np' file
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬nΪҪÏÔʾµÄÐкš£
ËÄ¡¢Ê¹ÓÃsedÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦ÀíµÄʾÀý£º
ͳ¼ÆÈÕÖ¾ÖзºÆðij¸öÒªº¦×ÖµÄÐÐÊý£º
sed -n '/keyword/p' logfile | wc -l
µÇ¼ºó¸´ÖÆ
½«ÈÕÖ¾ÖеÄʱ¼äÃûÌÃͳһ£º
sed 's/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/[hh:mm:ss]/g' logfile
µÇ¼ºó¸´ÖÆ
ɾ³ýÈÕÖ¾ÖеĿÕÐУº
sed '/^$/d' logfile
µÇ¼ºó¸´ÖÆ
½áÂÛ£º
ÔÚLinuxÇéÐÎÖУ¬Ê¹ÓÃsedÏÂÁî¿ÉÒÔ¶ÔÈÕÖ¾Îļþ¾ÙÐиßЧµÄÆÊÎöºÍ´¦Àí¡£±¾ÎÄÏÈÈÝÁËsedÏÂÁîµÄ»ù±¾Ê¹Ó㬲¢ÌṩÁËһЩ³£ÓõÄsedÏÂÁîʾÀý£¬¹©¶ÁÕ߲ο¼¡£Í¨¹ýÎÞаÔËÓÃsedÏÂÁ¿ÉÒÔ´ó´óÌá¸ß¶ÔÈÕÖ¾ÎļþµÄ´¦ÀíЧÂÊ£¬ÊµÏÖÈÕÖ¾µÄÆÊÎöºÍ´¦ÀíÄ¿µÄ¡£Ï£Íû±¾ÎĶԶÁÕßÄÜÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÖÐʹÓÃsedÏÂÁî¾ÙÐÐÈÕÖ¾ÆÊÎöºÍ´¦Àí£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡