####################################################################
# sed ¤Ë´Ø¤¹¤ë¥á¥â
####################################################################
=======================================================================
´ðËÜŪ¤ÊÎã
=======================================================================
¢£´ðËÜ·Á
sed 'ed ¥³¥Þ¥ó¥É¤Î¥ê¥¹¥È' ¥Õ¥¡¥¤¥ë̾
ÆþÎϹԤϰì¹Ô¤º¤Ä½èÍý¤µ¤ì¤ë
¹Ô¤òÌá¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤
ÆþÎÏ¥Õ¥¡¥¤¥ë¤ÎÆâÍƤÏÊѹ¹¤·¤Ê¤¤
ÆþÎÏ¥Õ¥¡¥¤¥ë¤ò»ØÄꤷ¤Ê¤±¤ì¤Ð¡¤É¸½àÆþÎϤ¬ÆþÎϤ·¤ÆÍøÍѤµ¤ì¤ë
½ÐÎϤÏɸ½à½ÐÎϤØ
¢£ÃÖ´¹¡§'UNIX' ¢ª 'UNIX(TM)'
sed 's/UNIX/UNIX(TM)/' foo.txt > bar.txt #°ì¹Ô¤Ë°ì²ó¤À¤±
sed 's/UNIX/UNIX(TM)/g' foo.txt > bar.txt #g ¥ª¥×¥·¥ç¥ó¤Ç²¿²ó¤Ç¤â
sed 's/UNIX/&(TM)/g' foo.txt > bar.txt #Ʊ¾å & ¤ò»È¤Ã¤¿
¢£¾ò·ï¤Ë¥Þ¥Ã¥Á¤¹¤ë¹Ô¤À¤±¤ÇÃÖ´¹
sed '/./s/UNIX/UNIX(TM)/g' foo.txt > bar.txt
¡Ê¶õ¹Ô°Ê³°¤Ç½èÍý¤¹¤ë¡§ /./ ¤Ï¶õ¹Ô°Ê³°¤Ë¥Þ¥Ã¥Á¤¹¤ë¡Ë
¢£¾ò·ï¤Ë¥Þ¥Ã¥Á¤·¤Ê¤¤¹Ô¤À¤±¤ÇÃÖ´¹
sed '/^$/!s/UNIX/UNIX(TM)/g'
¡Ê¶õ¹Ô°Ê³°¤Ç½èÍý¤¹¤ë¡§/^$/ ¤Ï¶õ¹Ô¤Ë¥Þ¥Ã¥Á¤¹¤ë¤«¤é¡Ë
¢£»ØÄê¹Ô¤Îɽ¼¨
sed -n '/UNIX/p'
¡ÊUNIX ¤ò´Þ¤à¹Ô¤À¤±É½¼¨¡§p ¥³¥Þ¥ó¥É¤Ï°õºþ¡¤
-n ¥ª¥×¥·¥ç¥ó¤Ïɸ½à¤Î½ÐÎϤÎÍÞÀ©¡Ë
sed '/UNIX/q'
¡ÊUNIX ¤ò´Þ¤à¹Ô¤Þ¤Çɽ¼¨¡§q ¥³¥Þ¥ó¥É¤Ï½ªÎ»¡Ë
¢£»ØÄê¹Ô¤Îºï½ü
sed '/UNIX/d'
¡ÊUNIX ¤ò´Þ¤à¹Ô¤ò½ÐÎϤ·¤Ê¤¤¡§d ¤Ïºï½ü¥³¥Þ¥ó¥É¡Ë
sed -n '/UNIX/!p'
¡Ê¾å¤ÈƱ¤¸¤À¤¬¡¤°ã¤¦ÊýË¡¡Ë
¢£É½¼¨¤¹¤ë¹Ô¤ÎÈϰϤλØÄê
sed -n '20,30p' # 20¹Ô¤«¤é30¹Ô¤Þ¤Ç¤òɽ¼¨
# *,* ¤Ï ed ¤Î¥³¥Þ¥ó¥É¤Î¥¢¥É¥ì¥¹¤Î»ØÄê¤Ç¤¢¤ë
sed '1,10d' # 1¹Ô¤«¤é10¹Ô¤Þ¤Ç½ÐÎϤ·¤Ê¤¤
sed '1,/^$/d' # ºÇ½é¤Î¶õÇò¹Ô¤Þ¤Çɽ¼¨¤·¤Ê¤¤
sed -n '/^$/,/^end/p' # ¶õ¹Ô¤«¤é end ¤Ç»Ï¤Þ¤ë¹Ô¤Þ¤Çɽ¼¨
sed '$d' # ºÇ½ª¹Ô¤òɽ¼¨¤·¤Ê¤¤
¢£Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¤Ë½ñ¤½Ð¤¹
$ sed -n '/pat/w file1
> /pat/!w file2' foo.txt
# /pat/ ¤Ë¥Þ¥Ã¥Á¤¹¤ì¤Ð file1, ¤½¤¦¤Ç¤Ê¤±¤ì¤Ð file2 ¤Ë½ñ¤½Ð¤¹
sed 's/UNIX/UNIX(TM)/gw u.out' foo.txt > output.txt
#ÃÖ´¹¤Î·ë²ÌÁ´ÂΤò output.txt ¤Ë½ñ¤½Ð¤·¡¤ÃÖ´¹¤¬¹Ô¤ï¤ì¤¿¹Ô¤À¤±¡Ê·ë²Ì¡Ë
#¤¬ u.out ¤Ë½ñ¤½Ð¤µ¤ì¤ë
=======================================================================
sed ¾ÜºÙ
=======================================================================
¢£ -n ¥ª¥×¥·¥ç¥ó
¥Ç¥Õ¥©¥ë¥È¤Î½ÐÎϤòÍÞÀ©¤¹¤ë
¡Ê¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¤¹¤Ù¤Æ¤Î¥Ñ¥¿¡¼¥ó¥¹¥Ú¡¼¥¹¤òɸ½à½ÐÎϤ˽ÐÎϤ¹¤ë¡Ë
¢£ -f ¥ª¥×¥·¥ç¥ó
¥¹¥¯¥ê¥×¥È¤ò¥Õ¥¡¥¤¥ë¤«¤éÆɤ߹þ¤à
ex) sed -f scriptfile < input.txt > output.txt
¢£ sed ¤Î¥³¥Þ¥ó¥É¤Î´ðËÜ·Á
[¥¢¥É¥ì¥¹] ¥³¥Þ¥ó¥É [°ú¿ô]
¥¢¥É¥ì¥¹¤Ï°Ê²¼¤Î¤è¤¦¤Ê·Á¼°
a,b a ¹Ô¤«¤é b ¹Ô¤Þ¤Ç
a a ¹Ô
a, b ¤Ë¤Ï¹Ô¿ô¤½¤Î¤â¤Î¤ò»ØÄꤷ¤Æ¤âÎɤ¤¤·¡¤Àµµ¬É½¸½¤ò»ÈÍѤ·¤Æ¤âÎɤ¤¡£
Àµµ¬É½¸½¤ò»ÈÍѤ·¤¿¾ì¹ç¡¤ºÇ½é¤Ë¤½¤Î RE ¤Ë¥Þ¥Ã¥Á¤¹¤ë¹Ô¤¬¥¢¥É¥ì¥¹¤È¤·
¤Æ»ÈÍѤµ¤ì¤ë¡£
¥¢¥É¥ì¥¹¤¬»ØÄꤵ¤ì¤Ê¤±¤ì¤Ð¡¤¥³¥Þ¥ó¥É¤Ï¥Ç¥Õ¥©¥ë¥È¤Î¹Ô¤ËÂФ·¤ÆºîÍѤò
µÚ¤Ü¤¹¡£sed ¤Ç¤ÏÁ´¤Æ¤Î¹Ô¤ËÂФ·¤Æ½èÍý¤¬¹Ô¤ï¤ì¤ë¤È»×¤Ã¤Æ¤¤¤¤¤À¤í¤¦¡£
¥³¥Þ¥ó¥É¤Ï 1 ʸ»ú
\n ¤¬²þ¹Ôʸ»ú¤Î¥¨¥¹¥±¡¼¥×¥·¡¼¥±¥ó¥¹¤È¤·¤ÆÍøÍѤǤ¤ë¡£
¢£Á´¥³¥Þ¥ó¥É¥ê¥¹¥È
¢£¥³¥á¥ó¥È
# # °Ê¹ß¤Ï¥³¥á¥ó¥È¤È¤·¤Æ°·¤ï¤ì¤ë
¢£ÄɲÃ
a\ ¹Ô¤ÎÄɲáÊÊ£¿ô¹ÔÄɲ乤ë¤È¤¤Ë¤Ï¥Æ¥¥¹¥È¤Î¹ÔËö¤Ë \ ¤¬É¬ÍסË