Linux¾«×¼É¾³ýÎļþĩβÐн̳Ì
ÔÚLinuxϵͳÖУ¬ÓÐʱ¼äÎÒÃÇÐèÒª¶ÔÎı¾Îļþ¾ÙÐо«×¼µÄ²Ù×÷£¬ºÃ±Èɾ³ýÎļþµÄĩβÐС£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃÏÂÁîÐй¤¾ßÀ´ÊµÏÖÕâ¸öÄ¿µÄ£¬°üÀ¨ÏêϸµÄ´úÂëʾÀý¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪʹÓÃÒ»¸öÇ¿Ê¢¶øÎÞаµÄÎı¾´¦Àí¹¤¾ß¡ª¡ªsed¡£sedÊÇÒ»¸öÁ÷±à¼Æ÷£¬¿ÉÒÔÔÚÏÂÁîÐÐÖоÙÐÐÎı¾µÄ´¦ÀíºÍ±à¼¡£ÎÒÃÇ¿ÉÒÔʹÓÃsedÏÂÁîÀ´É¾³ýÎļþµÄĩβÐС£
¼ÙÉèÎÒÃÇÓÐÒ»¸öÎı¾Îļþexample.txt£¬ÆäÖаüÀ¨¶àÐÐÎı¾ÄÚÈÝ¡£ÎÒÃÇÏ£Íûɾ³ý¸ÃÎļþµÄ×îºóÒ»ÐС£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÊµÏÖ£º
sed -i '$d' example.txt
µÇ¼ºó¸´ÖÆ
ÈÃÎÒÃÇÀ´ÏêϸڹÊÍÒ»ÏÂÕâ¸öÏÂÁ
sed£ºÌåÏÖŲÓÃsedÏÂÁî¡£
-i£ºÌåÏÖÖ±½ÓÔÚÔÎļþÉϾÙÐÐÐ޸ģ¬¶ø²»ÊÇÔÚÆÁÄ»ÉÏÏÔʾЧ¹û¡£
$d£ºÕâÊÇsedµÄÒ»¸öÏÂÁ$ÌåÏÖ×îºóÒ»ÐУ¬dÌåÏÖɾ³ý¸ÃÐС£
Ö´ÐÐÕâ¸öÏÂÁîºó£¬example.txtÎļþµÄ×îºóÒ»Ðн«±»É¾³ý¡£ÐèҪעÖصÄÊÇ£¬Õ⽫ֱ½ÓÐÞ¸ÄÔÎļþ£¬ÒÔÊÇÇëÎñ±ØÔÚ²Ù×÷Ç°±¸·ÝÎļþÄÚÈÝ¡£
ÈôÊÇÎÒÃÇÏëҪɾ³ýµ¹ÊýµÚ¶þÐУ¬¿ÉÒÔÉÔ×÷Ð޸ģº
sed -i '$-1d' example.txt
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁîÖУ¬$-1ÌåÏÖµ¹ÊýµÚ¶þÐС£Ö´Ðкó£¬example.txtÎļþµÄµ¹ÊýµÚ¶þÐн«±»É¾³ý¡£
³ýÁËʹÓÃsedÏÂÁîÍ⣬ÎÒÃÇ»¹¿ÉÒÔÍŽáÆäËûÏÂÁîʵÏÖɾ³ýÎļþĩβÐеIJÙ×÷¡£ºÃ±È£¬ÎÒÃÇ¿ÉÒÔʹÓÃheadÏÂÁîÍŽáÊä³öÖض¨ÏòÀ´ÊµÏÖ£º
head -n -1 example.txt > temp.txt mv temp.txt example.txt
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄhead -n -1ÌåÏÖÏÔʾÎļþÄÚÈݳýÁË×îºóÒ»ÐеÄÆäËûÐУ¬È»ºó½«Ð§¹ûÖض¨Ïòµ½Ò»¸öÔÝʱÎļþtemp.txtÖС£×îºó£¬½«temp.txtÖØÃüÃûΪexample.txt£¬¼´¿ÉʵÏÖɾ³ýÎļþĩβÐеIJÙ×÷¡£
×ܽáÆðÀ´£¬Í¨¹ýsedÏÂÁî»òÕßÍŽáÆäËûÏÂÁÔÚLinuxϵͳÖпÉÒÔºÜÀû±ãµØʵÏÖ¶ÔÎļþĩβÐеľ«×¼É¾³ý²Ù×÷¡£µ±ÐèÒª¶ÔÎı¾Îļþ¾ÙÐÐÐÞ¸Äʱ£¬ÕâЩÏÂÁ»áÊǺÜÊÇÓÐÓõŤ¾ß¡£Ï£ÍûÕâƪÎÄÕÂÄÜ×ÊÖúÄã¸üºÃµØÃ÷È·²¢ÔËÓÃÕâЩÏÂÁî¡£
ÒÔÉϾÍÊÇLinux¾«×¼É¾³ýÎļþĩβÐн̵̳ÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡