HOME > é»ç® > sed ã¡ã¢
sed ã¡ã¢
sed ã¡ã¢ïŒç¬¬äºçïŒããããŸãããã¡ããèŠãŠäžãããïŒ2015-5-26ïŒ
2014-10-04 äžéšæŽæ°
ç§ã¯ãã®å°ããªåãè ã倧奜ãã ã
gnu.org ã«ãã "sed, a stream editor" (http://www.gnu.org/software/sed/manual/sed.html) ãäœããšæçšãæ¥æ¬èªèš³ããhttp://www.bookshelf.jp/texi/sed/sed-ja_toc.html ã«ãã£ããã
ç®æ¬¡
sed ãšæ¹è¡
ãã¡ãã«æ°çããããŸãïŒ2015-5-26ïŒ
(1) å ¥åã«ãªãæ¹è¡ã®åºå
å ¥åã«ãªãæ¹è¡ã®åºåã¯ãæ¹è¡æåãããã¯ã¹ã©ãã·ã¥ã§é ãã°ããã
s/xyz/&\ /
ã®åŠãã(äŸã¯ xyz ã®åŸã«æ¹è¡ãæ¿å ¥ããã& ã¯ãããå šäœã瀺ããªã)
(2) 空è¡ã®åé€
空è¡ã®åé€ã¯ãæ¹è¡ã ãåé€ããããšããã«ãè¡å šäœã d ã³ãã³ãã§åé€ãããšèããã°å®¹æã
/^$/d
ã®åŠãã
(3) æ¹è¡ã«ãããããã
æ¹è¡ã«ããããããã«ã¯ãããããæãå¿ èŠãsed ã¯å ¥åãäžè¡ãã€èªã¿èŸŒãããã«ããã®æ£èŠè¡šçŸã¯è¡æ«ã«ããæ¹è¡ã«ãããããªãä»æ§ã ããã§ããã
sed ã¯èªã¿èŸŒãã è¡ã®è¡æ«ã«ããæ¹è¡ãåé€ããŠãã¿ãŒã³ã¹ããŒã¹ãšåŒã°ãããããã¡ã«æ ŒçŽããããã§ããã¹ãåŠçãããæåŸã«ãã¿ãŒã³ã¹ããŒã¹ã®å 容ã«æ¹è¡ãã€ããŠåºåããããšã®ããšã§ãã
The \n symbol does not match the newline at an end-of-line because when sed reads each line into the pattern space for processing, it strips off the trailing newline, processes the line, and adds a newline back when printing the line to standard output. (http://www.student.northpark.edu/pemente/sed/sedfaq3.html)
N ã³ãã³ãã䜿ã£ãŠã次è¡ããã¿ãŒã³ã¹ããŒã¹ã®å 容ã«è¿œå ãããšããã¿ãŒã³ã¹ããŒã¹ã®éäžã«ããæ¹è¡ã \n ã«ããããããN ã³ãã³ãã«ã€ããŠã®ããããã®èª¬æã¯ãã¡ãã
è©Šã¿ã« abcã®åŸã«æ¹è¡ãç¶ãå Žåããã®æ¹è¡ãåé€ããããšããŠã¿ãã
abc edf
ãªãå ¥åã«å¯Ÿãã
/abc$/N s/\n//
ãšããã³ãã³ããå®è¡ããã°
agcdef
ãšåºåãããã
ããããããå ¥åã
abc abc def
ã®åŠãããã°ãN ã³ãã³ã s ã³ãã³ãã¯ããããäžåºŠããå®è¡ããã¬ã®ã§ãåºåã¯
abcabc def
ã®ããã«ãªããäžæ¬æãªãã§ãããããã
:a /abc$/N /abc$/b a s/\n//g
ã®ããã«ããã°ãææã®ç®çãéããããã§ããããããã¯ãN ã³ãã³ãã«ããè¡ãé£çµããåŸããããããŠãã¿ãŒã³ã¹ããŒã¹ã®æåŸã« abc ããããã©ããã調ã¹ãããããã§ããã° b ã³ãã³ãã§ã©ãã« a ããªãã¡ã¹ã¯ãªãããã¡ã€ã«ã®äžè¡ç®ã«æ»ããŠåã³é£çµäœæ¥ãè¡ãããã以äžé£çµãã¹ãè¡ããªããªã£ãå Žåã«æ¹è¡ãåé€ããã
ããããäŸã«ãããŠã¯ãè¡æ«ã« abc ãšããç®å°ãã£ãã®ã§ããããç®å®ã«åŸç¶è¡ããã¿ãŒã³ã¹ããŒã¹ã«è¿œå ãããã©ããã®éžæããããšãã§ããã
ãããããè¡é ã« abc ãããå Žåã«ããã«å è¡ããæ¹è¡ãåé€ãããããšãããã©ãã§ãããããããè¡ãèªã¿èŸŒãã æç¹ã§ã¯ããã¿ãŒã³ã¹ããŒã¹ã«æ¬¡ã®è¡è¿œå ããã°ãããã©ããã¯åãããªãã
ããããæ¹æ³ãããã
:a N s/abc\n/abc/ t a P D
ããã¯ããšãããã次è¡ã N ã³ãã³ãã§é£çµããŠããŸããs ã³ãã³ãã®çœ®æãå©çšã㊠abc ã«ç¶ãæ¹è¡æåã®åé€ãè©Šã¿ããt ã³ãã³ãã§çœ®æã®æåŠãå€å¥ãã眮æã倱æããå ŽåãP ã³ãã³ãã§ãã¿ãŒã³ã¹ããŒã¹ã®äžããåã蟌ãŸããæ¹è¡ä»¥åãããªã³ãããD ã³ãã³ãã§ãã®éšåããã¿ãŒã³ã¹ããŒã¹ããåé€ããïŒD ã³ãã³ãã«ã€ããŠã®ããããã®èª¬æã¯ãã¡ãïŒããããŠããŸãå é ã«æ»ã£ãŠæ¬¡è¡ãèªã¿èŸŒãã眮æãæåããå Žåãã©ãã« a ãç®å°ã«å é ã«æ»ããåŠçãç¶ããã
ããããã«ã1 è¡ã¶ããã€å ¥ãæ¿ããªããåžžã« 2 è¡ã¶ãããã¿ãŒã³ã¹ããŒã¹ã«ä¿æããããã«ããŠããã®ã§ããã
(è£è¶³ 1) ãã t ã³ãã³ãããªããšã©ããªãããs ã³ãã³ããæåãããšæ¹è¡ãåé€ãããŠããŸãã®ã§ãD ã³ãã³ãããã¿ãŒã³ã¹ããŒã¹å 容ãå šéšåé€ããŠããŸããããã§ã¯ s ã³ãã³ãã«èŠéãããæ¹è¡ãã§ãŠããŠããŸãã
(è£è¶³ 2) D ã³ãã³ãã¯ããããããªãããŒãªåãããããåã蟌ãŸããæ¹è¡ãŸã§ããã¿ãŒã³ã¹ããŒã¹ããåé€ããã®ã¡ããŸã ãã¿ãŒã³ã¹ããŒã¹ã«äœãæ®ã£ãŠãããªãã°ãæ°ããªå ¥åè¡ãèªã¿èŸŒãããšãªã次ã®ãµã€ã¯ã«ãå§ããã®ã§ããã
ãã®ããæ¹ã¯ãæ±çšæ§ãé«ãããabc ã«å è¡ããæ¹è¡ã®åé€ãã®ã¿ãªãããabc ã«åŸç¶ããæ¹è¡ãåé€ããšããæåã®èª²é¡ãé£ç¡ãããªããŠããããããã«ããabc ãš def ã®éã®æ¹è¡ãåé€ããããšãããããªå€æãç°¡åã ããã ããããã¯æ¹è¡ãåé€ãããããªçœ®æãããã®ã§ãããs ã³ãã³ãã§ããããããã¿ãŒã³ã«æ¹è¡ãå«ãŸããŠããªãå Žåãã眮æã®çµææ¹è¡ã®æ°ãå¢å ãããããªçœ®æã§ã¯äºæãã¬åãããããsed ãè¡ãåäœãšããŠåããã®ã§ãã以äžã眮æã«ããè¡ãå¢æžãããå Žåã«ã¯æ éã«ãããªããŠã¯ãããªãã
次ã«ããabc ã§ãããè¡ãš def ã§ã¯ããŸãè¡ã®éã«ã空è¡ãæ¿å ¥ããããšãã課é¡ãèããŠã¿ããããã¯ã眮æã«ãã£ãŠãã¿ãŒã³ã¹ããŒã¹äžã®è¡æ°ãå¢å ããŠããŸããšããã±ãŒã¹ã§ããã
/\n/{P D} N s/abc\ndef/abc\ \ def/ P D
ã®ããã«ãããšããŸããããæåã® 2 è¡ã¯ããã¿ãŒã³ã¹ããŒã¹ã®äžã«åã蟌ãŸããæ¹è¡ãããå Žåã«ãå é ãããã¡ã°ãåŸãã«ããæ¹è¡ãŸã§ãåºåããããã(ãã¿ãŒã³ã¹ããŒã¹ãã)åé€ãããã®ã§ãããD ã³ãã³ããå®è¡ããããšãå¶åŸ¡ãå é ã«ç§»ãã®ã§ããã® 2 è¡ã¯ã«ãŒããšããŠåããæçµè¡ãšãã®å㧠P, D ã³ãã³ããå®è¡ããŠããã®ãäžèŠç¡é§ã«èŠãããããããªãããããããªããšãå¶åŸ¡ãå é è¡ã«ç§»ãåã«ããã¿ãŒã³ã¹ããŒã¹ã®å 容ãåãåºãããŠããŸãã
ããã«æ±çšæ§ãé«ãããããšãããšãä¹±æŽãªããšãããªããŠã¯ãªããªãã ããããã ãã¿ããã«å ¥åãã¡ã€ã«äžã®ãã¹ãŠã®è¡ãé£çµããŠãã¿ãŒã³ã¹ããŒã¹ã«è©°ã蟌ã¿ãäžæ°ã«çœ®æããŠããŸãã®ã ã
:a $!N $!b a s/\nabc/abc/g
ã¯ããã® 3 è¡ã«ãã£ãŠã匷åŒã«å ¥åãã¡ã€ã«ã®å 容ãäžã€ã«ã€ãªããŠãã¿ãŒã³ã¹ããŒã¹ã«æŒã蟌ãã§ãããïŒæçµè¡ã ãã¯å¥ãã€ããããŠããããããã¯æçµè¡ã§ N ã³ãã³ããå®è¡ãããšãããã§ãã¿ãŒã³ã¹ããŒã¹ã®å 容ãåºåããŠçµäºããŠããŸãã以äžã®çœ®æã³ãã³ããå®è¡ãããªãããã ãïŒ
ãã®ã¯ããã® 3 è¡ããããŸããªãã«æžããŠããã°ãå€ãã®å Žåããsed ã®æ£èŠè¡šçŸã¯è¡æ«ã®æ¹è¡ã«ãããããªãããšããå¶éãæ€å»ããããšåããããªå¹æãããã ããïŒïŒïŒããã ããå Žåã«ãã£ãŠã¯ã¡ã¢ãªã足ããªããªãå¯èœæ§ããããããã sed ã®å¶éãªã®ããsed ã䜿ããã¡ã¢ãªã®å¶éãªã®ãã¯ãã©ããã sed ãã©ãããç°å¢ã§äœ¿ã£ãŠãããã«ãããããšãããã¡ã¢ãªã足ããªããªããšããå±éºã¯æ¿ç¥ããŠããã¹ãã§ããã
ãŸããããšãã¡ã¢ãªãåå䜿ãããšããŠããããã€ã€ãŸããç°å¢ã§èªåšã« sed ãæã£ãŠãŠãã匷è ã«æ¬æãè¡šããããã«ã¯ããã£ãšèŠåŽããŠã¹ã¯ãªãããæžãã¹ãã§ããã :-)
ãšããã§ãsed ã«ã¯ãã¿ãŒã³ã¹ããŒã¹ã®ã»ãã«ãããŒã«ãã¹ããŒã¹ãšãããããã¡ããããããã«æååãäžæçã«éé¿ã§ããã ãã®ãããã¡ã§ãããããã¿ãŒã³ã¹ããŒã¹ã®å 容ããã¡ãã«å ¥ãããåŒãåºãããããããšã«ãã£ãŠãããã«é£ããã±ãŒã¹ã«å¯Ÿå¿ããã¹ã¯ãªãããæžãããšãã§ããããå Žåã«ãã£ãŠã¯ãã®ã»ããããèªç¶ãªæžãæ¹ãªããšããããã段èœå æ¹è¡ã®åé€ãã§ã¯ããããçšããŠããã
2 è¡ã«ããããã¿ãŒã³ã«ãããããã
æ¹è¡ãç¡èŠããŠæ¢çŽ¢ãã2 è¡ã«ãŸããããã¿ãŒã³ã«å¯ŸããŠãããããããããšããå Žåããããããšãã°ã
____abc_____a bc__abc____ab c___abc____ab
ãšããå ¥åã«å¯ŸããŠãabc ã xyz ã«å€æããŠåºåãããã
____xyz_____xyz __xyz____xyz ___xyz____ab
ãŠãªãµãã«ã
ãã£ãããªã®ã¯ãabc ã®éã«æ¹è¡ãã¯ããŸã£ãŠããç®æãããç¹ã§ãããäŸã«ãã£ãŠ sed ã¯äžè¡ãã€ã®åŠçãååã ãããçŽ æŽã«ãããš 2 è¡ã«ããããã¿ãŒã³ã«ã¯ãããããªãã®ã ã
(1) ãªã·ã£ã¬ãªããæ¹
ããã¯ãã¡ã¢ãªãã»ãã®å°ããã䜿ããªãã
# 1 è¡ã«ãããŸããã¿ãŒã³ã®çœ®æ s/abc/xyz/g N # 2 è¡ã«ããããã¿ãŒã³ã®çœ®æ s/ab\nc/xyz\ / s/a\nbc/xyz\ / P D
ã§ã¯ãã¡ãã£ãšèª¬æã
èµ€åïŒãããã¯å€ªåïŒã«ããã³ãã³ãããåŠçã®æµãã»ãããã¡ãŒãžã®èªã¿èŸŒã¿ã»åºåãå¶åŸ¡ããããã®ã³ãã³ããã¡ãç·åïŒãããã¯æäœïŒã®éšåã¯ãabc ã xyz ã«çœ®æããããã® s ã³ãã³ãã§ããã
æµãã¯ä»¥äžã®ããšããäžè¡ã«ãããŸã£ãŠãããã¿ãŒã³ã眮æïŒããããã® s ã³ãã³ãïŒã次ã®è¡ããã¿ãŒã³ã¹ããŒã¹ã«è¿œå ãïŒN ã³ãã³ãïŒã2è¡ã«ãŸããããã¿ãŒã³ã眮æïŒ2,3 çªç®ã® s ã³ãã³ãïŒããã¿ãŒã³ã¹ããŒã¹ã«åã蟌ãŸããæ¹è¡ä»¥åãããªã³ãïŒP ã³ãã³ãïŒããããã§åé€ïŒD ã³ãã³ãïŒããŸããã¯ããã«ãã©ãã
äžèšã®äŸã§äœãèµ·ãã£ãŠãããã詳ããèŠãŠã¿ãã
ã³ãã³ã | ãã¿ãŒã³ã¹ããŒã¹å 容 | åºåå 容 |
---|---|---|
1è¡ã«ãããŸããã¿ãŒã³ã®çœ®æ | 1 è¡ç® | |
N | 1 è¡ç®ãæ¹è¡ã2 è¡ç® | |
2è¡ã«ãŸããããã¿ãŒã³ã®çœ®æ | 1 è¡ç®ãæ¹è¡ã2 è¡ç® | |
P | 1 è¡ç®ãæ¹è¡ã2 è¡ç® | 1 è¡ç®ãæ¹è¡ã |
D | 2 è¡ç® | |
1è¡ã«ãããŸããã¿ãŒã³ã®çœ®æ | 2 è¡ç® | |
N | 2 è¡ç®ãæ¹è¡ã3 è¡ç® | |
2è¡ã«ãŸããããã¿ãŒã³ã®çœ®æ | 2 è¡ç®ãæ¹è¡ã3 è¡ç® | |
P | 2 è¡ç®ãæ¹è¡ã3 è¡ç® | 2 è¡ç®ãæ¹è¡ã |
D | 3 è¡ç® | |
1è¡ã«ãããŸããã¿ãŒã³ã®çœ®æ | 3 è¡ç® | |
N | 3 è¡ç® | 3 è¡ç®ãæ¹è¡ã |
çµäº |
ïŒè£è¶³1ïŒsed ã§ã¯ããµã€ããã¹ã¯ãªããæçµè¡ãŸã§ãããšå¶åŸ¡ãå é è¡ã«ãã€ãã次ã®å ¥åè¡ãèªåçã«èªã¿èŸŒãŸãããããããD ã³ãã³ãã䜿ããããšãå¶åŸ¡ãå é ã«æ»ãã¯ãããããã¿ãŒã³ã¹ããŒã¹ã空ã§ãªãéãèªåçã«è¡ãèªã¿èŸŒãŸãããã¯ããªãã
ïŒè£è¶³2ïŒN ã³ãã³ãã¯ãèªã¿èŸŒãã¹ã次ã®è¡ããªããšããã¿ãŒã³ã¹ããŒã¹ãåãåºã㊠sed ãçµäºãããã
ïŒè£è¶³3ïŒs ã³ãã³ãã N ã³ãã³ãã®ååŸäºç®æã«åããŠæžããããŠããçç±ããããæåã® s ã³ãã³ããã2, 3 çªç®ã® s ã³ãã³ãã®çŽåã«çœ®ããšãäžè¡ã«ãããŸã£ãŠãããã¿ãŒã³ã«å¯ŸããŠäºå眮æã³ãã³ããå®è¡ãããŠããŸãããã®çµæãabc ã aabc ã«çœ®æããããããªå Žåãaaabc ã«ãªã£ãŠããŸãã
ïŒè£è¶³4ïŒããã¯åºæ¬çã«ã¯ãåã«ç€ºããäŸãšåãèãæ¹ã§ããããs ã³ãã³ããæ¹è¡ãæ¶å»ãããå¢ããããããªãããšãåæã«ããŠããããã«ãã³ãŒãã£ã³ã°ãç°¡åã«ãªã£ãŠããã
(2) 匷åŒãªããæ¹
以äžã®ããæ¹ã¯ãå ¥åã®åéã«æ¯äŸããŠã¡ã¢ãªäœ¿çšéãå¢ããã¡ã¢ãªã足ããªããšã€ãã€ããšã«ãªãã
:a $!N $!b a # 1 è¡ã«ãããŸããã¿ãŒã³ã®çœ®æ s/abc/xyz/g # 2 è¡ã«ããããã¿ãŒã³ã®çœ®æ s/a\nbc/xyz\ /g s/ab\nc/xyz\ /g
ãã®ã¹ã¯ãªããã§ã¯ãå ¥åããã¹ãŠäžã€ã«ã€ãªããŠãã¿ãŒã³ã¹ããŒã¹ã«ã¶ã¡èŸŒã¿ãããããããã眮æãè¡ã£ãŠããã
段èœå æ¹è¡ã®åé€
ïŒãã®é ç®ã¯ãç§ã sed ã䜿ãããã«ãªã£ãã°ããã®ããæžããã¡ã¢ã§ãããããè¡ãããã«ããããªå€§ä»°ãªã¹ã¯ãªãããæžãå¿ èŠã¯ãªãã®ã§ãããŸãããèšå¿µã«åé€ããªãã§ãããã®ã§ãããã£ãšç°¡æœãªããæ¹ããsed ã¡ã¢ïŒç¬¬äºçïŒã®æ®µèœå ã®æ¹è¡ãåé€ãããïŒãã®1ïŒ ãã㳠段èœå ã®æ¹è¡ãåé€ãããïŒãã®2ïŒã«ãããŸãã 2015-5-16ïŒ
äžè¡20åã®åçš¿ãæžããšãã«ã20åããšã«æ¹è¡ãå ¥ãã人ãããããããŠãããããå Žåãè¡é ã®å šè§ã¹ããŒã¹ãæ¹æ®µèœã®èšå·ãšãªã£ãŠããããšãå€ããã¯ãŒããã®åæ°èšå®ãç¥ããªã人ã«å€ããã¿ãŒã³ã§ããã
ããã sed ã§ããµã€ãã®åçš¿ãã«ãªãããåŠçããŠã¿ãããã€ãŸããæ¹è¡ã¯æ®µèœã®æåŸã®ã¿ã«ã€ãããã«ã段èœå ã®æ¹è¡ãåé€ããŠè¡ãã€ãªããŠããã®ã§ããããã€ã®ãšããããã㯠sed åããªäœæ¥ã§ã¯ãªãããªããªãã°ãsed ã¯è¡æåã®ãšãã£ã¿ã§ããããããŠãæ¹è¡ã¯è¡ã®çµããã瀺ããã€ãŸããè¡å ç·šéãåŸæ㪠sed ã¯ãäºã€ä»¥äžã®è¡ãã€ãªãããšããäœæ¥ãèŠæãªã®ã ã
ããããèŠæãšããã©ãã§ããªãããã§ã¯ãªããããŒã«ãã¹ããŒã¹ãšãããã®ã䜿ããšããªããªãã¹ãããªãã£ãŠããããïŒã¹ããŒã¹ã®ãšãããèŠããªãããã³ããŒïŒããŒã¹ãããŠäœ¿ããããã«ããã®ãŸãŸã«ããŠããããïŒã
ã¹ããŒã¹ãå«ãŸãªã空è¡ã¯ããŸã£ããåé€ãããŠããŸãã®ã§æ³šæã
#!/bin/sed -nf # æçµè¡ ${ H x s/\n//g p b } # ã¹ããŒã¹ã§å§ãŸãæçµè¡ä»¥å€ã®è¡ /^[ã ]/{ x s/\n//g p b } # ã¹ããŒã¹ã§å§ãŸããªãæçµè¡ä»¥å€ã®è¡ H
ãŸããã¡ãŒã«ã«ãããããã¿ãŒã³ã«ã空è¡ã段èœã®åºåãã«ããŠãæ¹è¡ã¯é©å®èªã¿ãããããã«å ¥ãããšããããšãããã LaTeX çšã®åçš¿ãããã®ãããªåœ¢åŒã«ãªã£ãŠãããåçš¿ããã®æã®ãã®ã§ããã°ããã¯ã sed ã§åŠçã§ããã
åæ§ã®ããšã ed ã§è¡ãããšãã§ããããšãã€ãã§ã«èšã£ãŠãããã
泚æãä»¥äž sed çšã¹ã¯ãªããã¯ã空è¡ãããã¯åé€ããããŸããã¹ããŒã¹æåã ããããªãè¡ã空è¡ãšããŠæ±ãã
#!/bin/sed -nf # æçµè¡ ${ H x s/\n//g p b } # ç©ºè¡ /^[ã ]*$/{ x s/\n//g /[^ ã]/p b } # ãã以å€ã®è¡ H
ãã ããä»¥äž 2 äŸã«ã€ããŠã¯ ed ã§è¡ããšããã£ãšç°¡åããã
sed ã§æ¹è¡ã³ãŒãã®å€æ
ãã¡ãã«æ°çããããŸãïŒ2015-5-26ïŒ
æ¹è¡ã³ãŒãã sed ã§å€ããããªãããšãã話ããã°ãã°è³ã«ãããLF ãš CR ãš CR LF ãšã®çžäºå€æãšãã話é¡ã§ãããããã sed ã§ããã«ã¯ãããã€ãåé¡ç¹ããããUnix/Linux ãªç°å¢ã«ãããå ŽåãèããŠã¿ãã
第äžã®åé¡ã¯ãsed ãåºæ¬çã«ã¯å°å·å¯èœãªããã¹ããçžæã«ãããã®ã§ããããšãæ¹è¡ã瀺ããšã¹ã±ãŒãã·ãŒã±ã³ã¹ãã¹ã¯ãªããäžã«æžãããšã¯ããã¹ãŠã® sed ãèœãããããšã§ã¯ãªããGNU sed ã ãšã\x ãçšãã 16 é²ã«ããè¡šèšãã§ãããããLF 㯠\x0aãCR 㯠\x0d ãšæžãããšãã§ããïŒåè http://www.gnu.org/software/sed/manual/sed.html#Escapes ïŒãããããããããããšã¯ãããšãã° Mac ã«ã¯ããããå ¥ã£ãŠãã sed ã§ã¯ã§ããªããïŒãã®åé¡ã¯ã·ã§ã«ã®æ©èœã䜿ã£ãŠåé¿ã§ããããšããããããšãã°ãbash ã§ã¯ $'\x0d' ãšæžã㊠CR ãè¡šããåŠããçšããã®ã§ãããã ããããã¯ã·ã§ã«ã«äŸåãããããšãã°ãç§ã®ãšããã§ã¯ãbash çšã«æžããã¹ã¯ãªããã sh ã§ã¯ããŸãåããªããšããéªæªãªããšãèµ·ãããïŒ
第äºã®åé¡ã¯ã第äžã®åé¡ãããæ·±å»ã§ãããsed ã¯ã¹ã¯ãªãããè¡ã«é©çšããåã«æ¹è¡ãåé€ããŠãåŠçåŸæ¹è¡ãã€ãå ããŠåºåããã®ãä»æ§ã§ããïŒãã¡ã€ã«æ«å°Ÿãé€ãïŒãåºæ¬çã«ã¯ãLinux ç°å¢ã§äœ¿ããã sed ãªãè¡æ«ã® LF ãåãé€ããŠçœ®æçã®åŠçãè¡ã£ãåŸ LF ãä»å ããŠåºåããããããã¯åããçŽãããsed ã§æ¹è¡ã³ãŒãã®å€æŽããå¯ãããããšèšã£ãŠãããããªãã®ã ããã ãããã®åé¡ã«æ©ãŸãããªãå€æããããäžäŸãæãããšãLinux äžã§ LF â CR LF ããããšãã¯ãCR ã ããå ããã°ãããã sed ã§ãå¯èœã ãïŒãã®åé¡ãåé¿ããããã«ããã¹ãŠã®è¡ãã€ãªããŠèªã¿èŸŒãã§åŠçããããšãã§ããã:a;$!N;$!ba ãšããäŸã®ããŸããªãã ããã ããã¡ãã®ããæ¹ã¯ã巚倧ãªãã¡ã€ã«ãåŠçããå Žååé¡ãèµ·ããã ããïŒ
第äžã®åé¡ã¯ãCR ã ãã®æ¹è¡ã¯ Linux ã§ã¯æ¹è¡ãšããŠèªèãããã«ãè€æ°è¡ãããªããã¡ã€ã«ããã®ãããé·ãè¡ 1 è¡ã ãšè§£éããããšããããšã ã巚倧ãªãã¡ã€ã«ã ãšãã¡ã¢ãªãé£ãã€ãããããããªãã
sed ãäœãæ¹è¡ã³ãŒããšããŠèªèãããã«ã¯ LF, CR, CRLF ã® 3 ã€ã®ãã¿ãŒã³ãããããŸããLF, CR, CRLF éã®çžäºã®å€æã« 6 éããããsed ã®çš®é¡ã®åé¡ãããã¯ã·ã§ã«ã®ãšã¹ã±ãŒãã·ãŒã±ã³ã¹ã®åãæ±ãã®åé¡ãããããã¡ã€ã«ãäžæ°ã«ãããã¡ã«èªã¿èŸŒãŸããå±éºã«ã€ããŠã®å€æãããââã§ãããããsed ã«ããæ¹è¡ã³ãŒãã®å€æŽã¯ãªããªã倧å€ã ã
Unix/Linux ç°å¢ã§ã®çµè«
ããã¡ã€ã«ãµã€ãºã倧ãããŠãã¡ã¢ãªäžè¶³ã«ãªããããã®éãã§æãéãããã€å€ãã®ç°å¢ã§çšæãããŠããããŒã«ãåªå ããŠäœ¿ããšã次ã®ããã«ãªã£ãã
tr æ匷ãã§ããLFâCRLFãCRâCRLF ã¯ã§ããªããLFâCRLF 㯠Perl ããã GNU sed ã§ã§ãããCRâCRLF ã¯ãtr 㧠CRâLF ã«ããŠãã Perl ããã GNU sed 㧠LF â CRLF ã«ããäºæ®µæ§ãã
(1) | LF â CR | tr "\n" "\r" |
perl -p -e 's/\n/\r/' | ||
(2) | LF â CRLF | sed -e 's/$/\x0d/' # GNU sed ã®ã¿ |
perl -p -e 's/\n/\r\n/' | ||
(3) | CRLF â LF | tr -d "\r" |
sed -e 's/\x0d$//' # GNU sed ã®ã¿ | ||
perl -p -e 's/\r//' | ||
(4) | CRLF â CR | tr -d "\n" |
perl -p -e 's/\n//' | ||
(5) | CR â LF | tr "\r" "\n" |
(6) | CR â CRLF | tr "\r" "\n" | sed -e 's/$/\x0d/' # GNU sed ã®ã¿ |
tr "\r" "\n" | perl -p -e 's/\n/\r\n/' |
ã³ãã³ããã¡
ããã¯ãåã³ãã³ãã«ãäœãæåŸ ããŠããããã瀺ãããšããããã§ã¯ãªããåã³ãã³ããå©çšãããšãããæåŸ ãè£åãããã®ã¯äœæ ããã説æãããšãããã®ã§ãããŸãã
n ã³ãã³ã
n ã³ãã³ããš N ã³ãã³ãã¯éããã®ã§ãããããã§ã¯ãn ã³ãã³ããèŠãŠã¿ããïŒN ã³ãã³ãã«ã€ããŠã¯åŸè¿°ïŒãè足ãªãã©ãn ã³ãã³ããš -n ãªãã·ã§ã³ã¯äœã®é¢ä¿ããªãã
n ã³ãã³ãã¯ãçŸåšã®ãã¿ãŒã³ã»ã¹ããŒã¹ã®å 容ãåãåºããŠã次ã®è¡ãèªã¿èŸŒãããã¡ããããã®ãšãã«çŸåšè¡ã®çªå·ãã€ã³ã¯ãªã¡ã³ãããããçŸåšã®ãã¿ãŒã³ã»ã¹ããŒã¹ã®å 容ãåãåºãåäœã¯ -n ãªãã·ã§ã³äœ¿çšæã«ã¯æå¶ãããã
ãããªãäŸãèŠãŠã¿ãã
å¥æ°è¡ã»å¶æ°è¡ã ãåºå
å ¥åãã¡ã€ã«ã®å¥æ°è¡ã ããåºåãããªã
n d
ãšããã°ããããªããš 2 æåã®ã¹ã¯ãªãããªãïŒ-n ãªãã·ã§ã³ã¯äžèŠïŒããã€ã説æã
# ãã¿ãŒã³ã¹ããŒã¹ã«ããã㪠1 è¡ãèªã¿èŸŒã n # ãã¿ãŒã³ã¹ããŒã¹ãåºåããŠæ¬¡è¡ããã¿ãŒã³ã¹ããŒã¹ã«èªã¿èŸŒã d # ãã¿ãŒã³ã¹ããŒã¹ãåé€ãã # ãã€ãéããã¿ãŒã³ã¹ããŒã¹ãåºåãããã«ã空ã
å¶æ°è¡ã ãåºåãããªãã
1d n d
ãããã-n ãªãã·ã§ã³äžèŠããã€ã説æã
# ãã¿ãŒã³ã¹ããŒã¹ã«ããã㪠1 è¡ãèªã¿èŸŒã 1d # ãã 1 è¡ç®ãªãã # ãã¿ãŒã³ã¹ããŒã¹ãåé€ãã # æ°ããªå ¥åè¡ãèªã¿èŸŒã¿ã # ã¹ã¯ãªããã®å é ã«å¶åŸ¡ãæ»ã n # ãã¿ãŒã³ã¹ããŒã¹ãåºåããŠæ¬¡è¡ããã¿ãŒã³ã¹ããŒã¹ã«èªã¿èŸŒã d # ãã¿ãŒã³ã¹ããŒã¹ãåé€ãã # ãã€ãéããã¿ãŒã³ã¹ããŒã¹ãåºåãããã«ã空ã
æãªäººã¯ãã3 ã®åæ°è¡ã®ãšãã ããã¢ãããšåºåããšãè©Šã¿ãããããâŠâŠãšæžããæã£ããã©ãã€ãèªåã§æžããŠããŸã€ããã¹ããã
n n s/.*/aho/
-n ãªãã·ã§ã³ã¯äžèŠãªãã
N ã³ãã³ã
æ°ããè¡ããã¿ãŒã³ã¹ããŒã¹ã«èªã¿èŸŒãã以åãã£ãå 容ãšãæ°ããèªã¿èŸŒãŸããå 容ã¯æ¹è¡æåã«ãã£ãŠåºåãããããšããã®ããéãäžéã®èª¬æã
äºæ³ãããéããN ã³ãã³ããè¡ããšãã«ã¬ã³ãè¡ãé²ãã
$ cat t.txt 123 456 789
ãšãããã¡ã€ã«ããããšãããã
$ sed -ne "N; 1p" t.txt
ããã¯ãäœãåºåããªãã1 è¡ç®ã«ãããŠãN ã³ãã³ããé©çšãããã®ã§ãã«ã¬ã³ãè¡ã 2 è¡ç®ã«ç§»ãããããã£ãŠããã®åŸã« 1 è¡ç®ãããªã³ãããïŒ1pïŒãšãã£ãŠãããé ãã®ã§ãããïŒçºå¿µã-n ãªãã·ã§ã³ã«ãããæ瀺çãªããªã³ãã³ãã³ããªãåºåãæå¶ããŠããïŒ
$ sed -ne "N; 2p" t.txt 123 456
N ã³ãã³ãã«ããã«ã¬ã³ãè¡ã 2 è¡ç®ã«ç§»ã£ãŠãã1 è¡ç®ã®å 容ã¯ãã¿ãŒã³ã¹ããŒã¹ã«ä¿æãããŠããããšããããã
$ sed -ne "N; 3p" t.txt
äžæè°ãªããšã«ãããã¯äœãåºåããªããN ã³ãã³ãã¯ãèªã¿èŸŒãã¹ãå ¥åããªããšãçŸåšã®ãã¿ãŒã³ã¹ããŒã¹ã®å 容ãåºåããŠåŠçãçµäºãããã«ã¬ã³ãè¡ã 3 è¡ç®ã«ããŠã¹ã¿ãŒããããµã€ã¯ã«ã¯ãããã£ã±ãªã«N ã³ãã³ãã«ã¶ã€ããããšããããèªã¿èŸŒãã¹ã 4 è¡ç®ãå ¥åã«ã¯ãªããããããªã N ã³ãã³ãã¯çŸåšã®ãã¿ãŒã³ã¹ããŒã¹ãåºåããŠããã»ã¹çµäºããããšããããåºå㯠-n ãªãã·ã§ã³ã§æå¶ãããŠããã®ã§ïŒâ»ïŒãçµå±äœãåºåããªãã§çµäºããŠããŸãã®ã ã
â» -n ãªãã·ã§ã³ãããå Žåãæ瀺çãªããªã³ãã³ãã³ãã«ãããªãåºåã¯è¡ãããªãããèªã¿èŸŒãã¹ã次è¡ããªãå Žåã«å®è¡ããã N ã³ãã³ããããã¿ãŒã³ã¹ããŒã¹ã®å 容ãåãåºãããšãåäœã¯ãããã§ãããæ瀺çãªããªã³ãã³ãã³ããã§ã¯ãªãã®ã§ãæå¶ãããã®ã ã
äœæé²
ãã£ãŒã«ãã»ã»ãã¬ãŒã¿ã«ãæ¹è¡ãã䜿ãã決ãŸã£ããã£ãŒã«ãæ°ã§ 1 ã¬ã³ãŒããšããŠããããŒã¿ã®åŠçã®äŸã
001 ã«ããã³ç· ç·æ§ æç¥ç 002 çæšãæ ç·æ§ æ±äº¬éœ 003 çæšãæ女 å¥³æ§ æ±äº¬éœ
ãªããŠããŒã¿ãå ¥åãšããŠåãåãããéœéåºçãæ§å¥ãæ°åãã®é ã«äžŠã¹å€ããŠåºåããŠã¿ãã
-n ãªãã·ã§ã³ãã€ããã«
N N N s/\(.*\)\n\(.*\)\n\(.*\)\n\(.*\)/\1\n\4\n\3\n\2/
ãšããããšã«ãªããN ã³ãã³ã㧠1 ã¬ã³ãŒãã¶ãããã¹ãŠãã¿ãŒã³ã¹ããŒã¹ã«çªã£èŸŒãã§ãããæ¹è¡ãç®å°ã«ãã£ãŒã«ãã®äžŠã¹æããè¡ã£ãŠããã
ãããã¬ã³ãŒãã»ãã¬ãŒã¿ãå ¥ã£ãŠãããªãã°ãawk ã§åŠçããã»ãã楜ãããã
D ã³ãã³ã
D ã³ãã³ãã¯ããã¿ãŒã³ã¹ããŒã¹ããåã蟌ãŸããæ¹è¡ä»¥åïŒæ¹è¡ãå«ãïŒãåé€ããã
$ cat t.txt 123 456
ãšãããã¡ã€ã«ããããšãããã
$ sed -ne "N;D;p" t.txt
ããã¯ã456 ãåºåããããªãã®ã ããå®éã¯äœãåºåãããªããD ã³ãã³ããå®è¡ããããšãå¶åŸ¡ãïŒããã°ã©ã ã®ïŒå é è¡ã«æ»ãã®ã§ããã®ããã°ã©ã ã«ãããŠã¯ p ã³ãã³ãã¯äžåºŠãå®è¡ãããªãã®ã§ããã
ã ãããã ãã§ã¯ãªããD ã³ãã³ãã¯ãã£ãšå¥¥ãæ·±ãã®ã ã
$ cat t.txt <1> <2> <3>
ãšãããã¡ã€ã«ã«å¯ŸããŠãå®éšãããã
$ sed -ne "1N;=;p;D" t.txt 2 <1> <2> 2 <2> 3 <3>
= ã¯ã«ã¬ã³ãè¡çªå·ã衚瀺ãããã®ã§ãåºåãããããããããã ãã1N ã«ããã1 è¡ç®ã®å°»ã«æ¹è¡ãã¯ãã㧠2 è¡ç®ãã€ãªããã®ã§ã= ã³ãã³ãã®ãšãã㧠2 ããp ã³ãã³ãã®ãšãã㧠<1>+æ¹è¡+<2> ãåºåãããã®ã¯å®¹æã«ãããã
ãã®åŸãD ã³ãã³ãã§ããã¿ãŒã³ã¹ããŒã¹ããã<1> + æ¹è¡ããåé€ãããå¶åŸ¡ãããã°ã©ã å é ã«æ»ããããã°ã©ã å é ã«æ»ã£ãã®ã ãããä»åºŠã¯ 3 è¡ç®ãèªã¿èŸŒãããšæããã = ã³ãã³ãã¯å床 2 ãåºåããããã«ã€ã¥ã p ã³ãã³ãããåã®ãµã€ã¯ã«ã§ D ã³ãã³ããæ¶ãæ®ãããã¿ãŒã³ã¹ããŒã¹åŸåã®å 容ãã€ãŸã <2> ã衚瀺ããŠããããã®çç±ã¯ããã ãD ã³ãã³ãã¯ãå¶åŸ¡ãããã°ã©ã å é ã«æ»ããããã®ãšãæ°ããè¡ãèªã¿èŸŒãŸããïŒãã€ã«ã¬ã³ãè¡ãã€ã³ã¯ãªã¡ã³ããããïŒã®ãææ¢ããã®ã§ãããã ãã話ã¯ããã ãã§ã¯ãªãã
次㫠D ã³ãã³ããå®è¡ããããšãããã¿ãŒã³ã¹ããŒã¹ã¯ç©ºã«ãªãïŒããã©ã¯ N ã³ãã³ããå®è¡ããããåã蟌ãŸããæ¹è¡ããã¿ãŒã³ã¹ããŒã¹äžã«ãªããããã¹ãŠãåé€ãããïŒãå¶åŸ¡ã¯ãŸãããã°ã©ã å é ã«æ»ããD ã³ãã³ãã§æ»ã£ãã®ã ãããæ°ããªå ¥åè¡ãã€ãŸã 3 è¡ç®ã¯èªã¿èŸŒãŸããªãããšæãã°ãããã§ã¯ãªãã次㮠= ã³ãã³ã㯠3 ãåºåãããã¿ãŒã³ã¹ããŒã¹ã®å 容ãå°å·ãã p ã³ãã³ãã <3> ãåºåããŠãããD ã³ãã³ãã§ããã°ã©ã å é ã«é£ãã ã«ãããããããæ°ããªè¡ãèªã¿èŸŒãŸããŠããã®ã ãããã¯ã次ã®çç±ã«ãããD ã³ãã³ããå®è¡ãããçµæãããã¿ãŒã³ã¹ããŒã¹ã空ã«ãªã£ãŠããŸã£ããšãã¯ãå¶åŸ¡ãããã°ã©ã å é ã«æ»ãããã®ãšãæ°ããè¡ãèªã¿èŸŒãŸããïŒãã€ã«ã¬ã³ãè¡ãã€ã³ã¯ãªã¡ã³ãããïŒã®ãææ¢ããªãã®ã§ããã
äžåºŠç®ã® D ã³ãã³ãã«ããããã¿ãŒã³ã¹ããŒã¹ã¯ç©ºã«ãªããå¶åŸ¡ã¯ããã°ã©ã å é ã«ç§»ããæ°ããªå ¥åè¡ãèªã¿èŸŒãããšãããããããããå ¥åè¡ã¯ãªãã®ã§ãããã©ã«ãã®åäœã«åŸã£ãŠããã®ããã»ã¹ã¯çµäºããã
ããŒã«ãã»ã¹ããŒã¹
sed ã¯ãã¿ãŒã³ã»ã¹ããŒã¹ã®ã»ãã«ããŒã«ãã»ã¹ããŒã¹ãšãããããã¡ããã€ãããã¯ãããã«æååãä¿åããããã«äœ¿ããã¹ããŒã¹ã§ãããŒã«ãã¹ããŒã¹ã«å¯ŸããŠåãã³ãã³ãã¯ããã¿ãŒã³ã»ã¹ããŒã¹ããããã«æååãåºãå ¥ãããã³ãã³ãã ãã§ãããéé¿æã®ãããªãã®ãšèããŠå·®ãæ¯ããªãããªããããŒã«ãã»ã¹ããŒã¹ã¯äžã€ãããªãã
h ãã¿ãŒã³ã»ã¹ããŒã¹ --> ããŒã«ãã»ã¹ããŒã¹ ã³ããŒïŒäžæžãïŒ H ãã¿ãŒã³ã»ã¹ããŒã¹ --> ããŒã«ãã»ã¹ããŒã¹ ã³ããŒïŒè¿œå ïŒ g ãã¿ãŒã³ã»ã¹ããŒã¹ <-- ããŒã«ãã»ã¹ããŒã¹ ã³ããŒïŒäžæžãïŒ G ãã¿ãŒã³ã»ã¹ããŒã¹ <-- ããŒã«ãã»ã¹ããŒã¹ ã³ããŒïŒè¿œå ïŒ x ããŒã«ãã»ã¹ããŒã¹ <--> ãã¿ãŒã³ã»ã¹ããŒã¹ 亀æ
ããŒã«ãã»ã¹ããŒã¹ãžã®æååã®åºãå ¥ãã¯ãD ã³ãã³ãã N ã³ãã³ãã®ããã«å¶åŸ¡ã®æµããå€ããããšããªãã®ã§ãç解ããã®ãããé£ãããªãã
ããŒã«ãã»ã¹ããŒã¹ãå©çšããå žåçãªäŸã¯ãè¡ã«ãã£ãŠããŒã¿ã®æ§è³ªãç°ãªãå ¥åã®åŠçã§ããã
å¥æ°è¡ãšå¶æ°è¡ã®å ¥ãæ¿ã
å¥æ°è¡ãšå¶æ°è¡ãå ¥ãæ¿ããããšãèãžããããã¯ã眮æã³ãã³ãã䜿ããªãæå¿«ã§çŽ æ©ãããæ¹ã§ãããããããããªãããŒã«èŠãããããããªãã眮æã䜿ãæ°ãªããããŒã«ãã¹ããŒã¹ã䜿ããã«åæ§ã®ããšãã§ããã®ã§ããäœæé²ããåç §ããããã
-n ãªãã·ã§ã³ã€ãã§ã
h n p g p
ãšããã°ããããã®ãããåªæã£ãœãïŒãsed ã£ãŠæ¥œãããªããšããæãã§ãããã§ã¯èª¬æã
# ãŸããæåã«æ°ããè¡ããã¿ãŒã³ã¹ããŒã¹ã«èªã¿èŸŒãŸãã h # ããã€ãããŒã«ãã¹ããŒã¹ã«ã³ããŒããšã n # -n ãªãã·ã§ã³ã®ãããã§ãã¿ãŒã³ã¹ããŒã¹ãåºåããã«ãããã« # 次ã®äžè¡ããããã«ãã¿ãŒã³ã¹ããŒã¹ã«èªã¿èŸŒã p # ããã§ããã¿ãŒã³ã¹ããŒã¹ãåºåããšã g # ãã£ãéé¿ãããšããããŒã«ãã¹ããŒã¹ã®å 容ããã¿ãŒã³ã¹ããŒã¹ã«ã³ã㌠p # ãã¿ãŒã³ã¹ããŒã¹ã®å 容ãåºå
è¡æ¹åã®ããŒã¿æ§é ã®ãããã®ãæ€çŽ¢
$ cat words.txt apple and color p.10 dog cat p.11 apple orange p.13 green red apple
ãšããåèªåž³ã§ãapple ãšããåèªãäœããŒãžã«åºãŠããã®ããç¥ãã«ã¯ã
$ sed -ne "/^p\./h; /apple/ {g; /^$/d; p}" words.txt p.11 p.13
ãšããã°ãããp. ã§ã¯ããŸãè¡ããããšããããããŒã«ãã»ã¹ããŒã¹ã«ä¿ç®¡ããŠããã以äžã®è¡ã§ apple ãæ¢çŽ¢ããèŠã€ãã£ããšãã«ããŒã«ãã»ã¹ããŒã¹ã«ãã£ãå 容ãåãåºãã®ã§ããã/^$/d ã¯ã1 è¡ç®ã®ã¿ã€ãã«ã«ãã apple ã®ããŒãžæ°ã«å¯ŸããŠããŒãžæ°ã®ãããã«ç©ºè¡ãåºåãããã®ãé²ãã§ããã
段èœå æ¹è¡ã®åé€ã®äŸã®å ¥åã«ãããŠã¯ã段èœé ã瀺ãèšå·ïŒå šè§ã¹ããŒã¹ïŒã®ããè¡ã¯ãã»ãã®è¡ãšã¯éãç¹å¥ãªæå³ã瀺ãè¡æ¹åã®æ§é ãæã£ãŠãããšèšãããšãã§ãããããã§ããã¯ãããŒã«ãã»ã¹ããŒã¹ã䜿ã£ãŠããã
äœè«
sed ã¯ãããŸã§ãäžè¡ãã€åŠçããã®ã身äžãªã®ã§ãåæ¹åã®ã¿ãªããè¡æ¹åã«æ§é ããã£ãŠãããããªããŒã¿ã«å¯ŸããŠéžæçãªåŠçãããã®ã¯åºæ¬çã«ã¯èŠæã§ããããããã課é¡ã¯ãå ¥åå šäœããããã¡ã«ä¿æããŠããŠãå é ã«åãã£ãŠã®æ¢çŽ¢ãå¯èœãª ed ã®ã»ããåããŠãããããããªãã
äžè¿°ã® apple åºçŸããŒãžãæ¢çŽ¢ããäŸã® ed çã¯ã
$ echo '1;/^p\./,$g/apple/?^p\.?p' | /bin/ed -s words.txt p.11 p.13
ãšããããšã«ãªãããGNU ed ã§è©Šãããæ®å¿µãªããä»ã® ed ãã©ããªã£ãŠãããç¥ããªããã1 è¡ç®ãã p. ã§ã¯ããŸãè¡ãæ¢çŽ¢ãããã®è¡ããæçµè¡ãŸã§ã®äžã§ãapple ãå«ãè¡ããã¹ãŠæ¢ããããããŠèŠã€ãã£ãåè¡ãããã¡ã€ã«å é æ¹åã«åãã£ãŠ p. ã§ã¯ããŸãè¡ãæ¢ãåºããèŠã€ãã£ãè¡ãããªã³ãããããšããæå³ã§ããã1; ã¯å¯Ÿè©±ç°å¢ã«ãããŠã«ã¬ã³ãè¡ãã©ãã«ãããããããªãããã/^p\./ ã¯ããããªããš apple ã p. ã§ã¯ããŸãã©ã®è¡ãããå ã«ããå Žåã«æåŸã«ãã p. ã§ã¯ããŸãè¡ãåºåãããŠããŸãã®ãé²ãããã§ããã
ed 㯠/æ£èŠè¡šçŸ/ ã§ãã¡ã€ã«æ«å°Ÿæ¹åãžã®æ¢çŽ¢ãã§ãããããã®ã»ãã«ã?æ£èŠè¡šçŸ? ã§ãã¡ã€ã«å é æ¹åãžã®æ¢çŽ¢ãã§ãããåŠç察象ã®ãã¡ã€ã«ããã¹ãŠæ ŒçŽããŠãããããã¡ãæã€ããã§ããããšã§ãsed ã«ã¯ç䌌ã®ã§ãã¬ããšãªãã
æ°åãã€ã³ã¯ãªã¡ã³ããã
sed ã¯å€æ°ãæããªãã®ã§ãæ°ããã®ãã«äœ¿ããªãããšæããããããã§ããªããhttp://www.gnu.org/software/sed/manual/sed.html#Examples ã« counting xxx ãšããäŸãããã€ãèŒã£ãŠããã®ã§åèã«ãªãã
ããŠãããã§ã¯
$ echo 199 | sed -f increment.sed 200
ãšãããããªããšãããããã
expr ã§ã䜿ããšããã€ãããã£ãšãããã ããã®äœæ¥ã sed ã§ãã£ãŠã¿ããšãç°¡åãªããšãããªãè€éã«ãªãã®ã§åã³ãããïŒãããªããïŒãæ¹éãšããŠã¯ã1 æ¡ç®ã 9 ã§ãªããªããããã« 1 ã足ããå šæ¡ã 9 ãªããããããã¹ãŠ 0 ã«çœ®æããŠãæ°åã®é ã« 1 ãæžãå ããããã以å€ã®å Žåã¯ã1 ã±ã¿ç®ããäžã®ã±ã¿ã«åãã£ãŠé£ç¶ããŠãã 9 ããã¹ãŠ 0 ã«çœ®ãæãã0 ã§ãªãéãã®ãã¡ã°ãäžã®æ¡ã« 1 ã足ãã
1 ã±ã¿ç®ããäžã®ã±ã¿ã«åãã£ãŠé£ç¶ãã 9 ããã¹ãŠ 0 ã«çœ®ãæããããšããäœæ¥ãåºãŠããã®ã ãããããæå€ã«é£ãããç§ã¯ããŒã«ãã¹ããŒã¹ã®äžè©±ã«ãªãããæ¹ããèãã€ããªãã£ãããhttp://www.gnu.org/software/sed/manual/sed.html#Increment-a-number ã«çŽ æµãªããæ¹ãèŒã£ãŠãããã€ãŸãã1 ã®äœããäžã«åãã£ãŠé£ç¶ããŠãã 9 ããã¹ãŠ _ ã«çœ®æããŠããã_ ã®ããäžã«ããæ°åïŒããã¯åœç¶ 9 以å€ã§ããïŒã 1 å¢ãã眮æãããïŒæäžäœã 9 ã®ãšãã ãããã®äžã®æ¡ãšã㊠1 ãã€ãå ããïŒãæåŸã«ãy ã³ãã³ãã䜿ãã_ ã 0 ã«çœ®æããã®ã§ããããã®ããæ¹ã¯ãããŒã«ãã¹ããŒã¹ã䜿ããªãããã«ãå¿çšç¯å²ãåºãã
以äžã¯æåã«ç§ãæãã€ããããµãããæ¹ã
#!/bin/sed -f /\([0-9]*\)[0-8]$/ { s/\([0-9]*\)8$/\19/ s/\([0-9]*\)7$/\18/ s/\([0-9]*\)6$/\17/ s/\([0-9]*\)5$/\16/ s/\([0-9]*\)4$/\15/ s/\([0-9]*\)3$/\14/ s/\([0-9]*\)2$/\13/ s/\([0-9]*\)1$/\12/ s/\([0-9]*\)0$/\11/ q } /^99*$/ { y/9/0/ s/^/1/ q } h s/\(.*[^9][^9]*\)99*$/\1/ s/\([0-9]*\)8$/\19/ s/\([0-9]*\)7$/\18/ s/\([0-9]*\)6$/\17/ s/\([0-9]*\)5$/\16/ s/\([0-9]*\)4$/\15/ s/\([0-9]*\)3$/\14/ s/\([0-9]*\)2$/\13/ s/\([0-9]*\)1$/\12/ s/\([0-9]*\)0$/\11/ x s/.*[^9]\(99*\)$/\1/ y/9/0/ H g s/\n//
ãSedWord 貧ææžå ¥éïŒsed ã§çµçïŒãïŒã§ã¯ããã®ä»çµã¿ãå©çšããŠãPostscript ã® DSC ã³ã¡ã³ãäžã§äœ¿ãããŒãžçªå·çãçæããŠããã
ââè¡ããšã«åºåã
ããšãã°ãããã¹ã10è¡ããšã«ã---------------ããæ¿å ¥ããããšãããæ¹éãšããŠã¯ã10è¡ãŸã§ N ã³ãã³ãã§ãã¿ãŒã³ã¹ããŒã¹ã«ãã蟌ãã§ããã10è¡ããŸã£ãããããåãåºãããšã«ããã°ããããªããæåŸã ãã¯10è¡ã«æºããªããŠããåãåºãããã
N N N N N N N N N N a\ --------------------------------------------
次é ã®ãäžè¡ã®åæ°ãããããããšäœµããŠäœ¿ããšçµæ§ãªæŽåœ¢ãã§ããã§ãããŸãã
20 è¡ããšã«åºåãããã«ã¯ãN ã 20 åæžããªããŠã¯ãããªããããã§ã¹ã¯ãªããäžã« 20 ãšæ°åã§æžããŠæžãŸããããšæã£ãããæ¡å€é¢åã ã£ãã
#!/bin/sed -f :a $ b b /\([^\n]*\n\)\{19\}/! { N b a } :b s/$/\ ---------------/ p
以äžã説æ
#!/bin/sed -nf :a # ã©ãã« $ b b # æçµè¡ãªã :b ã«ãžã£ã³ãããŠãã¿ãŒã³ã¹ããŒã¹ã®å 容ãåºåãã /\([^\n]*\n\)\{19\}/! { # ããããã¿ãŒã³ã¹ããŒã¹ã«åã蟌ãŸããæ¹è¡ã19åå ¥ã£ãŠããªããªã # ããã¯ã20è¡ã¶ãã«æºããªãã®ã§ããããã N # 次ã®è¡ãèªã¿èŸŒãã b a # ãããŠãã¹ã¯ãªããå é ïŒ:aïŒãžæ»ã } :b # ã©ãã« s/$/\ # ãããã3è¡ãå®è¡ãããã®ã¯ãæçµè¡ãèªã¿èŸŒãã ã ---------------/ # ãã¿ãŒã³ã¹ããŒã¹ã«äºã€ã®åã蟌ãŸããæ¹è¡ããããã®ã©ã¡ãã
ã¡ãã£ãšæ°ãã€ãããã®ã¯ãN ã³ãã³ãã¯ããæ¹è¡ïŒæ¹è¡ãªãã®æ¬¡ã®è¡ãããã¿ãŒã³ã¹ããŒã¹ã«è¿œå ããã®ã§ã20è¡ãããŸã£ããã©ããã¯ã19åã®æ¹è¡ãå«ãŸããŠãããã©ãã調ã¹ãªããŠã¯ãªããªãã19æ³20æ¥ã§ããªããªããå ¥åãæçµè¡ã§ãããã©ããã®å€æã¯ã$ ã§ã¯ããŸãè¡ã§ãªããããããã®ãšãã«ã¯ãã§ã«æçµè¡ã®å 容ã¯åã®ãµã€ã¯ã«ã® N ã³ãã³ãã«ãã£ãŠãã¿ãŒã³ã¹ããŒã¹ã«è©°ã蟌ãŸããŠãããšããç¹ã«æ³šæã
ãã®ä»çµã¿ãå¿çšããã®ãããsed ã§ããã¹ãããã¹ãã¹ã¯ãªããã«ããããã¯ãa2ps ãšã mpage ã®ãããªããšããsed ã ãã§ããã¹ã¯ãªããã
äžè¡ã®åæ°ããããã
äžè¡ 20 åã«ãããããã«ãæ¹è¡ãæ¿å ¥ããŠããã¹ããæŽåœ¢ãããã ãã®ã¹ã¯ãªãã㯠-n ãªãã·ã§ã³ïŒæ瀺ããªãéãåºåãæå¶ïŒãšãšãã«å®è¡ããããšã
ãªãããã®ã¹ã¯ãªããã§ã¯ããªãªãžãã«ã®ããã¹ãã«ãã£ãæ¹è¡ã¯ãã®ãŸãŸããããªããã€ãŸããæ¹è¡ã¯è¿œå ãããã ãã§ãåé€ãããããšã¯ãªãããã®ç¹ã§ Emacs ã® fill ããnkf -fãfmt -w ãšã¯åäœãç°ãªãïŒãããã¯ã空è¡ãäœã£ãŠãã以å€ã®æ¹è¡ããææžæ§é ãšã¯é¢ä¿ã®ãªããã®ãšããŠéæã«åé€ããïŒã
æ®å¿µãªãããASCII æåãæ¥æ¬æåãåããäžã€ãšæ°ããã®ã§ïŒç§ã®äœ¿ã£ãŠãã sed ã§ã¯ïŒãè±æ°åãæ··ãããšè¡ãçãããªã£ãŠããŸãã
#!/bin/sed -nf :a /^.\{,20\}[ããããïŒïœ.,)]*$/! s/^\(.\{20\}\)/\1\ / /[ïŒããïœ]/ s/\([ïŒããïœ(][ïŒããïœ(]*\)\n/\ \1/ s/\n\([ããããïŒïœ.,)][ããããïŒïœ]*\)/\1\ / P D b a
次ã¯ãæŽåœ¢ããäŸã
äŸãã°ãã®ãããªããã¹ãããã£ããšãããã ããšã®ããã¹ãã¯æ®µèœã®ãããã«ããæ¹è¡ã ãªããããããäºã¯ãããããããã¹ãã ãäžè¡ãäºååã«ãªãããã«ãæŽåœ¢ããããšãã ã§ãããå¥èªç¹ãæ¬åŒ§éããæé ã«æ¥ãå Žåã ãããåè¡ã«ã¶ãäžããŠçµã¿ãæ¬åŒ§éããæ æ«ã«ãããããªå Žåã«ã¯ã次è¡ã«è¿œãåºãã ãã
ãããã説æããŠã¿ããæ¹éãšããŠã¯ãé·ãè¡ã®åãã 20 åæ°ããŠããã®åŸã«æ¹è¡ãå ¥ããããããå¥èªç¹ãªã©ãè¡é ã«ããããæ¬åŒ§éããè¡æ«ã«ãããããªããæ¹è¡äœçœ®ãããããããããŠæ¿å ¥ããæ¹è¡ä»¥åãåºåããåŸãæ®ãã«å¯ŸããŠåãããšãç¹°ãè¿ãã以äžçž·ã èŠããªãã
01: #!/bin/sed -nf 02: :a ãžã£ã³ãå ã©ãã« 03: /^.\{,20\}[ããããïŒïœ.,)]*$/! s/^\(.\{20\}\)/\1\ 04: / 20å以äžïŒå¥èªç¹ã ããããªãè¡ã¯ããããªã ããã§ãªããã°è¡é ããæ°ããŠé£ç¶ãã20åã®åŸã«æ¹è¡ãæ¿å ¥ ãã®çµæããã¿ãŒã³ã¹ããŒã¹ã®äžèº«ã¯ ãããã¹ã20å+åã蟌ãŸããæ¹è¡+æ®ãããšãªã 05: /[ïŒããïœ]/ s/\([ïŒããïœ(][ïŒããïœ(]*\)\n/\ 06: \1/ ãããã¹ã+è¿œãåºãæå+æ¹è¡ãããããã¹ã+æ¹è¡+è¿œãåºãæåãã«çœ®æ ããããšã«ãããè¡æ«ã«æ¥ã [ïŒããïœ( ã次è¡è¡é ã«è¿œãåºãã ã¡ãªã¿ã«ããã¿ãŒã³ã¹ããŒã¹äžã«åã蟌ãŸããæ¹è¡ã¯ \n ããããããã æåã« /[ïŒããïœ]/ 㧠s ã³ãã³ãã®é©çšè¡ãéå®ããŠããããããã¯ã ç¶ãæ£èŠè¡šçŸã®ã³ã¹ããé«ããããå°ãã§ããããåæ°ãæžããããã ãªãã20æå以äžã®è¡ã¯ã03 è¡ç®ãé©çšãããªãã®ã§åã蟌ãŸããæ¹è¡ãæããã ãã®è¡ã® s ã³ãã³ãã®åœ±é¿ãåããªãïŒãã㯠07 è¡ç®ãåãããšïŒã 07: s/\n\([ããããïŒïœ.,)][ããããïŒïœ]*\)/\1\ 08: / ãããã¹ã+æ¹è¡+ã¶ãããæåãããããã¹ã+ã¶ãããæå+æ¹è¡ãã«çœ®æ ããããšã«ãããè¡é ã«æ¥ã ããããïŒïœ., ãåè¡ã«ã¶ãäžãã ïŒã¡ãªã¿ã« 05 è¡ç®ã® s ã³ãã³ãã®ãããããããã® s ã³ãã³ãã®ãããã®ã»ãã ã¯ããã«ã³ã¹ããäœããã¯ã€ã«ãã«ãŒãã®åã« \n ããããåŸãã« \n ããããã® å·®ããåŠçé床ã«å€§ããªåœ±é¿ãäžããââç§ã®äœ¿ã£ãŠãã sed ã§ã¯ïŒ 09: P ãã¿ãŒã³ã¹ããŒã¹ã®æ¹è¡ä»¥åãåºå åã蟌ãŸããæ¹è¡ããªããã°ããã¿ãŒã³ã¹ããŒã¹ããã¹ãŠãåºå 10: D ãã¿ãŒã³ã¹ããŒã¹ã®æ¹è¡ä»¥åãåé€ åã蟌ãŸããæ¹è¡ããªããã°ããã¿ãŒã³ã¹ããŒã¹ããã¹ãŠåé€ ãã®çµæãã¿ãŒã³ã¹ããŒã¹ã空ã«ãªããš11è¡ç®ã¯å®è¡ããã 次ã®å ¥åè¡ãèªãã§ãããã«å é ãããµã€ã¯ã«ãéå§ 11: b a ã¹ã¯ãªããå é ã«æ»ã
次ã®è¡ãèªã¿èŸŒãã§ããåã®è¡ã«å¯ŸããåŠçã決ãããšããå¿ èŠããªãã®ã§ãããŒã«ãã¹ããŒã¹ãå©çšãããæ¯èŒçã¹ãããªããã¹ã¯ãªããã«ãªã£ãŠããïŒãïŒïŒã/.*a/ ã®ãããªãããã¯ã/a.*/ ã«æ¯ã¹ãŠå€§å€ãªã³ã¹ãããããã®ã§ããããããã¯å®è£ ããããªãã¢ã«ãŽãªãºã ãæ¡çšããŠãããã«äŸåããŠããã
<é£æ¬²ã®åé¿(URLã®æœåº)
è¡ãåããèŠãŠãã£ãŠãæåã«åºãŠãã abc ããåã®éšåãåé€ããã«ã¯ã©ãããããããã 話ã®é åºãšããŠããããå§ããŠã¿ãã ããã¯ãsed ã§ã¯ããªãé¢åã§ãããããšãã°ã0abc1abc2abc3 ãããæåã® 0 ã ããåé€ããŠãabc1abc2abc3 ãšãããã ãã ãã0,1,2,3 ã®éšåããäºæž¬äžå¯èœãªãã¿ãŒã³ã§ãæ©èœããããã«ãããããabc ããã£ãšè€éãªè¡šçŸã§ãã£ãŠãèããããããã«èããŠã¿ããã
s/.*\(abc.*\)/\1/
ã ãšãè¡é ãããæåŸã«åºçŸãã abc ã®çŽåããŸã§ãåé€ãããŠããŸãã * ãæé·ããããããããã§ããã* ã¯è²ªæ¬²ã§ããããŸããé£ã¹ãã®ã§ããã ãã ãæåŸã® abc ãŸã§é£ã¹ããšãããã倱æããŠãŠãŒã¶ã«æãããããããããŸã§ã¯é£ã¹ãªãã ãŸããsed ã«ã¯ãabc ãšãããã¿ãŒã³ä»¥å€ããšããè¡šçŸã¯ãªããã ããããããéšå以åããšããè¡šçŸããªãã æåã«åºãŠãã abc ãæ¶å»ããã®ã¯ç°¡åã ãããããæ®ããŠããããåãæ¶ããšããã®ã¯é£ããã®ã§ããã ããããã°ã§ããã
s/abc/\ &/ s/.*\n//
æåã®çœ®æ(æåã®2è¡)ã§ãã¯ãããŠåºãŠãã abc ã®åã«æ¹è¡ãæ¿å ¥ãã次ã®çœ®æã§å é ããæ¹è¡ãŸã§ãåé€ããŠãããå ¥åã«ã¯ååšããªãã¯ãã®æ¹è¡ã³ãŒããç®å°ãšããŠäœ¿ã£ãŠããã®ã§ããã
ããŠãæåã® abc ã ãã§ãªããŠãäºçªç®ä»¥åŸã® abc ã次ã ãšåºåããã«ã¯ã©ããããããã ãããã
s/abc/\ &\ /g s/[^\n]*\n\([^\n]*\)\n/\1\ /g s/[^\n]*$//
1ã3è¡ç®ã§ãabc ãšãããã¿ãŒã³ã®ååŸã«æ¹è¡ãæ¿å ¥ã4ã5 è¡ç®ã§ã[ãã¿ãŒã³ã«ãããããªãæåå][æ¹è¡][ãã¿ãŒã³ã«ãããããæåå][æ¹è¡] ãšããäžé£ã®ãã®ãå é ããæ¢ããŠãããã [ãã¿ãŒã³ã«ãããããæåå][æ¹è¡] ã«å€æããŠãããæåŸã«ã[ãã¿ãŒã³ã«ãããããªãæåå] ãæ®ãããããããæ¶å»ããŠããã
URL ã®æœåº
ãŸããURL ãè¡šçŸããæ£èŠè¡šçŸã§ãããããããã®ãã®ã¯æ¬ã¡ã¢ã®èå³ããå€ããã®ã§ããŸãã¯ããããŒã«
^s\{0,1\}https\{0,1\}:\/\/[a-zA-Z0-9.$,;:&=?!*~@#_()\/]*
ãšããŠããããã¡ãããæ¥æ¬èªïŒµïŒ²ïŒ¬ããããæäžã ããããããäžè¶³ãªã®ã¯æããã
ææã®ç®çã®ããã«ã¯ãåé ã® abc ã®éšåã«åœãŠã¯ããŠã
s/s\{0,1\}https\{0,1\}:\/\/[a-zA-Z0-9.$,;:&=?!*~@#_()\/]*/\ &\ /g s/[^\n]*\n\([^\n]*\)\n/\1\ /g s/[^\n]*$//
ã§æžããããã䜿ã£ãŠ URL ã®æœåºããããããªã·ã§ã«é¢æ° extracturl ãäœããªãã°
extracturl (){ cat $1 | sed -e 's/s\{0,1\}https\{0,1\}:\/\/[a-zA-Z0-9.$,;:&=?!*~@#_()\/]*/\ &\ /g s/[^\n]*\n\([^\n]*\)\n/\1\ /g s/[^\n]*$//' | sed -e '/^$/d' }
ã®ããã«ãçµæãã空è¡ãåé€ããããã«ããŠããã°äœ¿ããããã
ç®å°ã䜿ããªãã§ãããšâŠâŠ
以äžã®ããæ¹ã¯ãæ¹è¡ã³ãŒããè¡äžã«ãªãããšãåæãšãããã®ã§ããããããã®åæã厩ããå Žåã«ã¯å¥ã®æ¹æ³ãå¿ èŠã«ãªã(N ã³ãã³ããå©çšãããšããªã©)ãå®éã«ã¯ãããããããªããŠã¯ãªããªãå Žåã¯ãå€ããªãã ãããããã£ããèããŠã¿ãã®ã ããæžããŠããã
ãŸããç®å°ããã£ãããã¿ãŒã³ã¹ããŒã¹æ¿å ¥ããªããšããæ¹éã§ã0abc1abc2abc3 ãšããæååãã abc ãšãããã¿ãŒã³ãæ€çŽ¢ããããšã«ãã£ãŠãå é ã® 0 ãæ¶å»ããã«ã¯ã©ããããããããèããã
:a /^abc/! s/^.// t a
ã®ããã«ããã°ãããŸãããã ãããããã¯ãå é ã abc ã§ãããã©ãããæ€æ»ããŠãNo ã§ããã°ãå é äžæåãæ¶å»ã®äžãããäžåºŠæ€æ»ãããæ€æ»ã®çµæã Yes ã§ããã°ãããã§çµäºããããã ããããã¯ããã«ãé¢åããããäœéã§ããããã絶察ã«å ¥åè¡ã«çŸããªããã¿ãŒã³ããšãããã®ããªããããªå Žåã«ã¯ãä»æ¹ããªãã
ã§ã¯ãã絶察ã«å ¥åè¡ã«çŸããªããã¿ãŒã³ããç®å°ãšããŠäœ¿ãããšãªãã0abc1abc2abc3 ãšããå ¥åããã3 ã€ã® abc ã ãåãåºãã«ã¯ã©ããããã
:s /^abc/! s/^.// t s h s/\(abc\).*/\1/p g s/abc\(.*\)/\1/ t s
ããŒã«ãã¹ããŒã¹ãå©çšããè€éãªåœ¢ãšãªã£ãã以äžèª¬æã
:s ã©ãã« /^abc/! s/^.// å é ã abc ã§ãªããªãå é äžæåãåé€ t s äžã®çœ®æãæåãããã©ãã« sïŒã¹ã¯ãªããå é ïŒã«æ»ã å é ã« abc ãåºãŠãããããã¯ãäžæåãæ®ã£ãŠããªããšãã«ã«ãŒãããæããããšã«ãªã h ãã¿ãŒã³ã¹ããŒã¹ãããŒã«ãã¹ããŒã¹ã«ã³ã㌠s/\(abc\).*/\1/p ãã¿ãŒã³ã¹ããŒã¹å é ã«ãã abc ã ããæ®ããŠä»ãåé€ããåºå g ããŒã«ãã¹ããŒã¹ã«ã³ããŒããŠããããã®ããã¿ãŒã³ã¹ããŒã¹ã«æ»ã s/abc\(.*\)/\1/ ãã¿ãŒã³ã¹ããŒã¹å é ã«ãã abc ã ããåé€ãã t s äžã®çœ®æãæåãããã©ãã« sïŒã¹ã¯ãªããå é ïŒã«æ»ã è¡æ«ã«ãããšãã¿ãŒã³ã¹ããŒã¹å é ã« abc ããªãã®ã§ãããã§ãã®è¡ã®åŠçãçµäºãã
ããã§ã¯ãããã¹ããã¡ã€ã«ãããURL ã ããæãåºãã¹ã¯ãªããã«ãããå¿çšããŠã¿ãããäžã§ abc ãšãããšããããURL ãè¡šçŸããæ£èŠè¡šçŸã«å€ããŠããïŒãã®æ£èŠè¡šçŸã¯å®å šã§ã¯ãªãïŒã
:s /^s\{0,1\}https\{0,1\}:\/\/[a-zA-Z0-9.$,;:&=?!*~@#_()\/]*/! s/^.// t s h s/\(s\{0,1\}https\{0,1\}:\/\/[a-zA-Z0-9.$,;:&=?!*~@#_()\/]*\).*/\1/p g s/s\{0,1\}https\{0,1\}:\/\/[a-zA-Z0-9.$,;:&=?!*~@#_()\/]*\(.*\)/\1/ t s
ãªãããã¯ãäºã€ã® sed ã³ãã³ãããã€ãã§ã€ãªãã°ããã£ãšç°¡åã«ã§ããïŒã¯ããã«èšãïŒãæåã® sed ããã»ã¹ã§ãURL ã®ååŸã«æ¹è¡ãæ¿å ¥ããŠã次㮠sed ããã»ã¹ã§ã¯ãURL ã«ãããããè¡ã ãåºåããŠããã®ã§ããã
cat some.txt | sed -e 's/s\{0,1\}https\{0,1\}:\/\/[a-zA-Z0-9.$,;:&=?!*~@#_()\/]*/\ &\ /g | sed -ne '/s\{0,1\}https\{0,1\}:\/\/[a-zA-Z0-9.$,;:&=?!*~@#_()\/]*/ p'
ããŸã
ãããèšè
ããšãã°ãabcãããcbaãã«å€æãããããŸãå ¥åæååã®æåŸã«æ¹è¡ãã€ããŠããããæ¹è¡ã®çŽåã® 1 æåãæ¹è¡ã®çŽåŸã«ãã£ãŠããããšããããšããæ¹è¡ãå é ã«åºãŠãããŸã§ç¹°ãè¿ãã
s/$/\n/ :a s/\([^\n]\)\(\n.*\)/\2\1/ ta s/\n//
sed ã§ããã£ã³ã°
ã·ã§ã«ã¹ã¯ãªããäžã§ã1 ã 001 ã«ã12 ã 012 ã«ã123 ããã®ãŸãŸã«ããããã€ãŸã 3 æ¡ã«ãªããããæ°åã®é ã«å¿ èŠãªæ°ã® 0 ãã€ãå ãããã
$ echo 123 | sed -e 's/^[0-9][0-9]$/0&/;t;s/^[0-9]$/00&/'
t ã¯ãå眮ãããã¢ãã¬ã¹ã§çœ®æãæåããå Žåã«ãåŸçœ®ãããã©ãã«ã«é£ã¶ãå眮ãããã¢ãã¬ã¹ããªããã°ãåœè©²è¡ãšè§£éãããåŸçœ®ãããã©ãã«ããªããã°ãã¹ã¯ãªããæ«å°Ÿãšè§£éããããã€ãŸããè€æ°ã® s ã³ãã³ããæžãããšãã«ãããããã®åŸãã«ãã¡ãã¡ t ãšã ãæžããŠããã°ãã¹ã¯ãªããé ãã s ã³ãã³ããå®è¡ããŠãã£ãŠã眮æãæåããããšããã§åŠçãçµäºãããããããã« 2 ã±ã¿ã®æ°åã 3 ã±ã¿ã«ãã s ã³ãã³ãã眮æã«æåããã°ã1 ã±ã¿ã®æ°åã 3 ã±ã¿ã«ãã s ã³ãã³ãã¯å®è¡ãããã«æžãã®ã§ãææã®ç®çãéæãããã
ãã¡ãããè¯ãå㯠sed ãªãã䜿ããã«ãprintf ã䜿ã£ãŠ
$ printf "%03d" 12 $ 012
ãªããŠãããšæããã©ã
Aãå«ãè¡ã®åè¡ã®BãCã«çœ®æãã
sed -ne '/A/{x;s/B/C/;x};x;2,$p;${g;p}'
ãã¿ãŒã³ã¹ããŒã¹ã«èªã¿èŸŒãŸãã n è¡ç®ãã A ãæ¢ããŠãããããããããŒã«ãã¹ããŒã¹ã«èããããŠãã n-1 è¡ç®ã眮æã1 è¡ç®ã§ã¯ /A/{x;s/B/C/;x}; ã¯äœãããªãããšã«æ³šæãn è¡ç®ãèªã¿èŸŒãã ãµã€ã¯ã«ã§ãããŒã«ãã¹ããŒã¹ã® n-1 è¡ç®ãããªã³ããããããã«ãªã£ãŠããããªããæçµè¡ãèªã¿èŸŒãã ãµã€ã¯ã«ã§ã¯ãæçµè¡ã®åã®è¡ã ãã§ãªããããŒã«ãã¹ããŒã¹ã«ç§»ãããŠããæçµè¡ãåºåãã(${g;p})ã