Bashã¹ã¯ãªããïŒéå§
Bashã¹ã¯ãªãããããŒã2ïŒã«ãŒã
Bashã¹ã¯ãªãããããŒã3ïŒãªãã·ã§ã³ãšã³ãã³ãã©ã€ã³ã¹ã€ãã
Bashã¹ã¯ãªãããããŒã4ïŒå
¥åãšåºå
Bashã¹ã¯ãªãããããŒã5ïŒã·ã°ãã«ãããã¯ã°ã©ãŠã³ãã¿ã¹ã¯ãã¹ã¯ãªãã管ç
Bashã¹ã¯ãªãããããŒã6ïŒé¢æ°ãšã©ã€ãã©ãªãŒã®éçº
Bashã¹ã¯ãªãããããŒã7ïŒsedãšã¯ãŒãã
Bashã¹ã¯ãªãããããŒã8ïŒawkããŒã¿åŠçèšèª
Bashã¹ã¯ãªããããŒã9ïŒæ£èŠè¡šçŸ
Bashã¹ã¯ãªãããããŒã10ïŒã±ãŒã¹ã¹ã¿ãã£
Bashã¹ã¯ãªãããããŒã11ïŒå¯Ÿè©±åãŠãŒãã£ãªãã£ã®æåŸ
ãšèªåå
ååãbashã¹ã¯ãªããã®é¢æ°ãç¹ã«ã³ãã³ãã©ã€ã³ããããããåŒã³åºãæ¹æ³ã«ã€ããŠèª¬æããŸããã ä»æ¥ã®ãããã¯ã¯ãæååããŒã¿ãåŠçããããã®éåžžã«äŸ¿å©ãªããŒã«ã§ããsedãšåŒã°ããLinuxãŠãŒãã£ãªãã£ã§ãã å€ãã®å Žåããã°ãã¡ã€ã«ãæ§æãã¡ã€ã«ãããã³ãã®ä»ã®ãã¡ã€ã«ã®åœ¢åŒã§ããã¹ããæäœããããã«äœ¿çšãããŸãã
bashã¹ã¯ãªããã§äœããã®æ¹æ³ã§ããŒã¿ãåŠçããå Žåã
sedããã³gawkããŒã«ãç解ããã®ã«å®³ã¯ãããŸããã ããã§ã¯ãsedãšããã¹ãã®æäœã«çŠç¹ãåœãŠãŸããããã¯ãbashã¹ã¯ãªãããéçºããåºå€§ãªç¯å²ãæ
ããäžã§éåžžã«éèŠãªã¹ãããã ããã§ãã
次ã«ãsedã®äœ¿çšã®åºæ¬ãåæãããã®ããŒã«ã®äœ¿çšäŸã30以äžãããŸãã
Sedã®åºæ¬
sedãŠãŒãã£ãªãã£ã¯ãã¹ããªãŒãã³ã°ããã¹ããšãã£ã¿ãŒãšåŒã°ããŸãã nanoãªã©ã®ã€ã³ã¿ã©ã¯ãã£ãããã¹ããšãã£ã¿ãŒã¯ãããŒããŒãã䜿çšããããã¹ãã®æäœããã¡ã€ã«ã®ç·šéãããã¹ãã®è¿œå ãåé€ãå€æŽãè¡ããŸãã Sedã䜿çšãããšãéçºè
ãèšå®ããäžé£ã®ã«ãŒã«ã«åºã¥ããŠããŒã¿ã¹ããªãŒã ãç·šéã§ããŸãã ãã®ã³ãã³ãã®åŒã³åºãã¯æ¬¡ã®ããã«ãªããŸãã
$ sed options file
ããã©ã«ãã§ã¯ãsedã¯ãäžé£ã®ã³ãã³ããšããŠè¡šãããåŒã³åºãã§æå®ãããã«ãŒã«ã
STDIN
ã«é©çšã
STDIN
ã ããã«ãããããŒã¿ãçŽæ¥sedã«è»¢éã§ããŸãã
ããšãã°ã次ã®ããã«ïŒ
$ echo | sed 's/test/another test/'
ãã®ã³ãã³ããå®è¡ãããšã次ã®ããã«ãªããŸãã
ã·ã³ãã«ãªsedåŒã³åºãã®äŸãã®å Žåãsedã¯ãåŠçã®ããã«æž¡ãããè¡ã®åèªãtestããåèªãanother testãã«çœ®ãæããŸãã åŒçšç¬Šã§å²ãŸããããã¹ããåŠçããããã®ã«ãŒã«ã®å®è¡ã«ã¯ãã¹ã©ãã·ã¥ã䜿çšãããŸãã ãã®å Žåã
s/pattern1/pattern2/
ãšãã圢åŒ
s/pattern1/pattern2/
ã³ãã³ã
s/pattern1/pattern2/
ãŸãã ãsããšããæåã¯ããsubstituteããšããåèªã®ç¥èªã§ããã€ãŸããåã«çœ®æã³ãã³ãããããŸãã ãã®ã³ãã³ããå®è¡ããSedã¯ã転éãããããã¹ããèŠãŠããã®äžã«èŠã€ãã£ããã©ã°ã¡ã³ãïŒä»¥äžã§èª¬æãããã®ïŒã
pattern1
ã«å¯Ÿå¿ãã
pattern2
ãŸãã
äžèšã¯ãsedã䜿çšããåºæ¬çãªäŸã§ãããé床ãäžããããã«å¿
èŠã§ãã å®éãsedã¯ãããšãã°ãã¡ã€ã«ã®æäœãªã©ãã¯ããã«è€éãªããã¹ãåŠçã¹ã¯ãªããã§äœ¿çšã§ããŸãã
以äžã¯ãããã¹ãã®æçãå«ããã¡ã€ã«ãšããã®ãããªã³ãã³ãã«ããåŠçã®çµæã§ãã
$ sed 's/test/another test' ./myfile
ããã¹ããã¡ã€ã«ãšãã®åŠççµæäžèšã§äœ¿çšããã®ãšåãã¢ãããŒããããã§é©çšãããŸãããsedã¯ãã¡ã€ã«ã«ä¿åãããããã¹ããåŠçããŸãã åæã«ããã¡ã€ã«ãååã«å€§ããå Žåãsedããããã§ããŒã¿ãåŠçãããã¡ã€ã«å
šäœãåŠçãããã®ãåŸ
ããã«ãåŠçããããã®ãç»é¢ã«è¡šç€ºããããšã«æ°ä»ãã§ãããã
Sedã¯ãåŠçäžã®ãã¡ã€ã«ã®ããŒã¿ãå€æŽããŸããã ãšãã£ã¿ãŒã¯ãã¡ã€ã«ãèªã¿åããèªã¿åããåŠçããäœãèµ·ãã£ããã
STDOUT
éä¿¡ã
STDOUT
ã ãœãŒã¹ãã¡ã€ã«ãå€æŽãããŠããªãããšã確èªããã«ã¯ãsedã«æž¡ãããåŸããããéããŠãã ããã å¿
èŠã«å¿ããŠãsedåºåããã¡ã€ã«ã«ãªãã€ã¬ã¯ãããããããå€ããã¡ã€ã«ãäžæžãã§ããŸãã å
¥åããã³åºåã¹ããªãŒã ã®ãªãã€ã¬ã¯ãã«ã€ããŠèª¬æããŠãããã®ã·ãªãŒãºã®ä»¥åã®
è³æã®ããããã«ç²ŸéããŠããå Žåã¯ããããè¡ãããšãã§ããŸãã
sedãåŒã³åºããããšãã«ã³ãã³ãã»ãããå®è¡ãã
è€æ°ã®ããŒã¿æäœãå®è¡ããã«ã¯ãsedãåŒã³åºããšãã«
-e
ã¹ã€ããã䜿çšããŸãã ããšãã°ã2ã€ã®ããã¹ãã®çœ®æãæŽçããæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
$ sed -e 's/This/That/; s/test/another test/' ./myfile
sedã§-eã¹ã€ããã䜿çšãããã¡ã€ã«ã®ããã¹ãã®åè¡ã«ã¯ãäž¡æ¹ã®ã³ãã³ããé©çšãããŸãã ãããã¯ã»ãã³ãã³ã§åºåãå¿
èŠããããŸãããã³ãã³ãã®æ«å°Ÿãšã»ãã³ãã³ã®éã«ã¯ã¹ããŒã¹ãå
¥ããªãã§ãã ããã
sedãåŒã³åºããããšãã«è€æ°ã®ããã¹ãåŠçãã³ãã¬ãŒããå
¥åããã«ã¯ãæåã®åäžåŒçšç¬Šãå
¥åããåŸãEnterãæŒããŠãããéãåŒçšç¬Šãå¿ããã«æ°ããè¡ããåã«ãŒã«ãå
¥åããŸãã
$ sed -e ' > s/This/That/ > s/test/another test/' ./myfile
ãã®åœ¢åŒã§è¡šç€ºãããã³ãã³ããå®è¡ãããåŸã次ã®ããã«ãªããŸãã
sedã䜿çšããå¥ã®æ¹æ³ãã¡ã€ã«ããã³ãã³ããèªã¿åã
ããã¹ãã®åŠçã«å¿
èŠãªsedã³ãã³ããå€æ°ããå Žåã¯ãéåžžããã¡ã€ã«ã«äºåã«æžã蟌ãã®ãæã䟿å©ã§ãã ã³ãã³ããå«ãsedãã¡ã€ã«ãæå®ããã«ã¯ã
-f
䜿çšããŸãã
mycommands
ãã¡ã€ã«ã®å
容ã¯æ¬¡ã®
mycommands
ã§ãã
s/This/That/ s/test/another test/
sedãåŒã³åºããã³ãã³ããå«ããã¡ã€ã«ãšåŠççšã®ãã¡ã€ã«ããšãã£ã¿ãŒã«æž¡ããŸãã
$ sed -f mycommands myfile
ãã®ãããªã³ãã³ããåŒã³åºãããšãã®çµæã¯ãåã®äŸã§åŸãããçµæã«äŒŒãŠããŸãã
sedãåŒã³åºããããšãã«ã³ãã³ãã§ãã¡ã€ã«ã䜿çšãã亀æããŒã ãã©ã°
次ã®äŸãããèŠãŠãã ããã
$ sed 's/test/another test/' myfile
ãã¡ã€ã«ã«å«ãŸãããã®ãšãsedã«ãã£ãŠåŠçãããåŸã«ååŸããããã®ã次ã«ç€ºããŸãã
ãœãŒã¹ãã¡ã€ã«ãšãã®åŠççµæ眮æã³ãã³ãã¯éåžžãè€æ°ã®è¡ã§æ§æããããã¡ã€ã«ãåŠçããŸãããåè¡ã§æ€çŽ¢ãããããã¹ããã©ã°ã¡ã³ãã®æåã®åºçŸã®ã¿ã眮æãããŸãã ãã³ãã¬ãŒãã®ãã¹ãŠã®åºçŸã眮ãæããã«ã¯ãé©åãªãã©ã°ã䜿çšããå¿
èŠããããŸãã
ãã©ã°ã䜿çšãããšãã«çœ®æã³ãã³ããèšè¿°ããã¹ããŒã ã¯æ¬¡ã®ããã«ãªããŸãã
s/pattern/replacement/flags
ãã®ã³ãã³ãã®å®è¡ã¯ãããã€ãã®æ¹æ³ã§å€æŽã§ããŸãã
- çªå·ã転éãããšããæååãžã®ãã³ãã¬ãŒãã®ãšã³ããªã®ã·ãŒã±ã³ã¹çªå·ãèæ
®ããããã®ãšã³ããªã¯çœ®ãæããããŸãã
g
ãã©ã°ã¯ããã®è¡ã«ãããã¿ãŒã³ã®ãã¹ãŠã®åºçŸãåŠçããå¿
èŠãããããšã瀺ããŸãã
p
ãã©ã°ã¯ããœãŒã¹æååã®å
容ã衚瀺ããå¿
èŠãããããšã瀺ããŸãã
w file
ãšãã圢åŒã®ãã©ã°ã¯ãããã¹ãåŠçã®çµæãw file
ã«æžã蟌ãããã³ãã³ãã«æ瀺ããŸãã
眮æã³ãã³ãã®æåã®ãªãã·ã§ã³ã®äœ¿çšãæ€èšããç®çã®ãã©ã°ã¡ã³ãã®çœ®æããããªã«ã¬ã³ã¹ã®äœçœ®ã瀺ããŸãã
$ sed 's/test/another test/2' myfile
眮æããããã©ã°ã¡ã³ãã®äœçœ®ã§çœ®æã³ãã³ããåŒã³åºãããã§ã¯ã眮æãã©ã°ãšããŠçªå·2ã瀺ããŸãããããã«ãããåè¡ã§2çªç®ã«åºçŸããç®çã®ãã³ãã¬ãŒãã®ã¿ã眮æããããšããäºå®ã«è³ããŸããã 次ã«ãã°ããŒãã«çœ®æãã©ã°
g
è©ŠããŠãã ããã
$ sed 's/test/another test/g' myfile
åºåãããããããã«ããã®ãããªã³ãã³ãã¯ãããã¹ãå
ã®ãã³ãã¬ãŒãã®ãã¹ãŠã®åºçŸã眮ãæããŸããã
ã°ããŒãã«çœ®æ眮æã³ãã³ã
p
ã®ãã©ã°ã䜿çšãããšãäžèŽãèŠã€ãã£ãè¡ã衚瀺ã§ããŸãããsedã®åŒã³åºãæã«æå®ããã
-n
ã¹ã€ããã¯éåžžã®åºåãæå¶ããŸãã
$ sed -n 's/test/another test/p' myfile
ãã®çµæããã®æ§æã§sedãèµ·åãããšãæå®ããããã¹ãã®æçãèŠã€ãã£ãè¡ïŒãã®å Žåã¯1è¡ïŒã®ã¿ã衚瀺ãããŸãã
replace pã³ãã³ããã©ã°ã®äœ¿çšw
ãã©ã°ã䜿çšããŠãããã¹ãåŠççµæããã¡ã€ã«ã«ä¿åã§ããŸãã
$ sed 's/test/another test/w output' myfile
ããã¹ãåŠççµæããã¡ã€ã«ã«ä¿åããã³ãã³ãã®æäœäžã«ããŒã¿ã
STDOUTã«åºåãããåŠçãããè¡ã
w
åŸã«ååãæå®ããããã¡ã€ã«ã«æžã蟌ãŸããããšãæ確ã«ããããŸãã
åºåãæå
/bin/bash
ã
/etc/passwd
/bin/csh
ã«çœ®ãæããå¿
èŠããããšæ³åããŠãã ããã ã¿ã¹ã¯ã¯ããã»ã©é£ãããããŸããïŒ
$ sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd
ãã ããããã¯ãã¹ãŠéåžžã«ããèŠããŸããã åé¡ã¯ãçŽæ¥ã¹ã©ãã·ã¥ãåºåãæåãšããŠäœ¿çšããããããsedæååã§ã¯åãæåããšã¹ã±ãŒãããå¿
èŠããããšããããšã§ãã ãã®çµæãããŒã ã®å¯èªæ§ãäœäžããŸãã
幞ããªããšã«ãsedã䜿çšãããšã眮æã³ãã³ãã§äœ¿çšããç¬èªã®åºåãæåãèšå®ã§ããŸãã åºåãæåã¯ã
s
åŸã«åºçŸããæåã®æåã§ãã
$ sed 's!/bin/bash!/bin/csh!' /etc/passwd
ãã®å Žåãæå笊ãã»ãã¬ãŒã¿ãŒãšããŠäœ¿çšãããŸãããã®çµæãã³ãŒãã¯èªã¿ããããªãã以åãããã£ãšãããã«èŠããŸãã
åŠçããããã¹ããã©ã°ã¡ã³ãã®éžæ
ãããŸã§ããšãã£ã¿ãŒã«æž¡ãããããŒã¿ã¹ããªãŒã å
šäœãåŠçããããã«sedãåŒã³åºããŸããã å Žåã«ãã£ãŠã¯ãsedã䜿çšããŠãããã¹ãã®ç¹å®ã®éšåïŒç¹å®ã®ç¹å®ã®è¡ãŸãã¯è¡ã®ã°ã«ãŒãïŒã®ã¿ãåŠçããå¿
èŠããããŸãã ãã®ç®æšãéæããã«ã¯ã2ã€ã®ã¢ãããŒãã䜿çšã§ããŸãã
- åŠçãããè¡ã®æ°ã«å¶éãèšå®ããŸãã
- åŠçããè¡ã«å¯Ÿå¿ãããã£ã«ã¿ãŒãæå®ããŸãã
æåã®ã¢ãããŒããæ€èšããŠãã ããã ããã§ã¯2ã€ã®ãªãã·ã§ã³ã䜿çšã§ããŸãã æåã«ã以äžã§èª¬æããããã«ãåŠçããå¿
èŠããã1è¡ã®æ°ã瀺ããŸãã
$ sed '2s/test/another test/' myfile
sedã®åŒã³åºãæã«èšå®ãããçªå·ã§ãã1è¡ã®ã¿ãåŠçãã2çªç®ã®ãªãã·ã§ã³ã¯è¡ã®ç¯å²ã§ãïŒ
$ sed '2,3s/test/another test/' myfile
è¡ç¯å²åŠçããã«ã眮æã³ãã³ããåŒã³åºããŠããã¡ã€ã«ãç¹å®ã®è¡ããæåŸãŸã§åŠçãããããã«ããããšãã§ããŸãã
$ sed '2,$s/test/another test/' myfile
2è¡ç®ããæåŸãŸã§ã®ãã¡ã€ã«åŠç眮æã³ãã³ãã䜿çšããŠãæå®ããããã£ã«ã¿ãŒã«å¯Ÿå¿ããè¡ã®ã¿ãåŠçããã«ã¯ã次ã®ããã«ã³ãã³ããåŒã³åºãå¿
èŠããããŸãã
$ sed '/likegeeks/s/bash/csh/' /etc/passwd
äžèšã§èª¬æããå
容ãšåæ§ã«ããã³ãã¬ãŒãã¯ã³ãã³ãå
s
åã«æž¡ãããŸãã
ãã£ã«ã¿ãŒãããã³ã°æååã®åŠçããã§ã¯ãéåžžã«åçŽãªãã£ã«ã¿ãŒã䜿çšããŸããã ãã®ã¢ãããŒãã®å¯èœæ§ãå®å
šã«æããã«ããããã«ãæ£èŠè¡šçŸã䜿çšã§ããŸãã ãããã«ã€ããŠã¯ããã®ã·ãªãŒãºã®æ¬¡ã®è³æã®ããããã§èª¬æããŸãã
è¡ãåé€
sedãŠãŒãã£ãªãã£ã¯ãæååã®1ã€ã®æåã·ãŒã±ã³ã¹ãä»ã®æåã·ãŒã±ã³ã¹ã«çœ®ãæããã®ã«é©ããŠããã ãã§ã¯ãããŸããã ã€ãŸãã
d
ã³ãã³ãã䜿çšããŠãããã¹ãã¹ããªãŒã ããè¡ãåé€ã§ããŸãã
ã³ãã³ãåŒã³åºãã¯æ¬¡ã®ããã«ãªããŸãã
$ sed '3d' myfile
3è¡ç®ãããã¹ãããåé€ããå¿
èŠããããŸãã ããã¯ãã¡ã€ã«ã§ã¯ãªãããšã«æ³šæããŠãã ããã ãã¡ã€ã«ã¯å€æŽãããªããŸãŸã§ãåé€ã¯sedã圢æããåºåã«ã®ã¿åœ±é¿ããŸãã
3è¡ç®ãåé€d
ãåŒã³åºããšãã«åé€ããè¡ã®çªå·ãæå®ããªãå Žåãã¹ããªãŒã å
ã®ãã¹ãŠã®è¡ãåé€ãããŸãã
d
ã³ãã³ããè¡ã®ç¯å²ã«é©çšããæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
$ sed '2,3d' myfile
è¡ã®ç¯å²ãåé€ãããããŠãæå®ãããè¡ãããã¡ã€ã«ã®æåŸãŸã§ã®è¡ãåé€ããæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
$ sed '3,$d' myfile
ãã¡ã€ã«ã®æåŸãŸã§ã®è¡ãåé€ããŸããã¿ãŒã³ã䜿çšããŠè¡ãåé€ããããšãã§ããŸãã
$ sed '/test/d' myfile
ãã¿ãŒã³ããšã«è¡ãåé€ããd
åŒã³åºããããšããã¿ãŒã³ã®ãã¢ãæå®ã§ããŸãããã¿ãŒã³ãå«ãè¡ãšãã®éã«ããè¡ã¯åé€ãããŸãã
$ sed '/second/,/fourth/d' myfile
ãã¿ãŒã³ã䜿çšããŠäžé£ã®è¡ãåé€ããã¹ããªãŒã ã«ããã¹ããæ¿å
¥ãã
sedã§ã¯ã
i
ããã³ã³ãã³ãã䜿çšããŠããã¹ãã¹ããªãŒã ã«ããŒã¿ãæ¿å
¥ã§ããŸãã
i
ã³ãã³ãã¯ãæå®ãããè¡ã®åã«æ°ããè¡ãè¿œå ããŸãã
a
ã³ãã³ãã¯ãæå®ãããè¡ã®åŸã«æ°ããè¡ãè¿œå ããŸãã
i
ã³ãã³ãã®äœ¿çšäŸãèããŠã¿ãŸãããã
$ echo "Another test" | sed 'i\First test '
ããŒã i次ã«ã
a
ã³ãã³ããèŠãŠã¿ãŸãããã
$ echo "Another test" | sed 'a\First test '
ããŒã Aã芧ã®ãšããããããã®ã³ãã³ãã¯ã¹ããªãŒã ã®ããŒã¿ã®ååŸã«ããã¹ããè¿œå ããŸãã éäžã«è¡ãè¿œå ããå¿
èŠãããå Žåã¯ã©ãããŸããïŒ
ããã§ã¯ãã¹ããªãŒã ãŸãã¯ãã³ãã¬ãŒãå
ã®åç
§è¡ã®çªå·ã瀺ãããšã§åœ¹ç«ã¡ãŸãã ç¯å²ãšããŠæååãã¢ãã¬ã¹æå®ããŠããããã§ã¯æ©èœããªãããšã«æ³šæããŠãã ããã
i
ã³ãã³ããåŒã³åºããŠãæ°ããè¡ãæ¿å
¥ããåã«è¡çªå·ãæå®ããŸãã
$ sed '2i\This is the inserted line.' myfile
åç
§è¡çªå·ã§ã³ãã³ãa
ã³ãã³ãã§ãåãããšãè¡ããŸãã
$ sed '2a\This is the appended line.' myfile
åç
§è¡çªå·ãæã€ã³ãã³ãaããŒã
i
ãš
a
äœæ¥ã®éãã«æ³šæããŠãã ããã æåã¯æå®ãããåã«æ°ããè¡ãæ¿å
¥ãã2çªç®ã¯æââå®ãããè¡ã®åŸã«æ¿å
¥ããŸãã
æååã®çœ®æ
c
ã³ãã³ãã䜿çšãããšãããŒã¿ã¹ããªãŒã ã®ããã¹ãè¡å
šäœã®å
容ãå€æŽã§ããŸãã åŒã³åºããšãã¯ãè¡çªå·ãæå®ããå¿
èŠããããŸãã代ããã«ãã¹ããªãŒã ã«æ°ããããŒã¿ãè¿œå ããå¿
èŠããããŸãã
$ sed '3c\This is a modified line.' myfile
æååå
šäœã®çœ®æã³ãã³ããåŒã³åºããšãã«ãã¬ãŒã³ããã¹ããŸãã¯æ£èŠè¡šçŸã®åœ¢åŒã®ãã³ãã¬ãŒãã䜿çšãããšããã³ãã¬ãŒãã«äžèŽãããã¹ãŠã®è¡ã眮ãæããããŸãã
$ sed '/This is/c This is a changed line of text.' myfile
ãã¿ãŒã³ã«ããæååã®çœ®æãã£ã©ã¯ã¿ãŒçœ®æ
y
ã³ãã³ãã¯åã
ã®æåãåŠçããåŒã³åºãäžã«æž¡ãããããŒã¿ã«åŸã£ãŠãããã眮ãæããŸãã
$ sed 'y/123/567/' myfile
ãã£ã©ã¯ã¿ãŒçœ®æãã®ã³ãã³ãã䜿çšãããšãããã¹ãã¹ããªãŒã å
šäœã«é©çšãããããšãèæ
®ããå¿
èŠããããŸããç¹å®ã®æåã®åºçŸã«å¶éããããšã¯ã§ããŸããã
åºåè¡çªå·
=
ã³ãã³ãã䜿çšããŠsedãåŒã³åºãããå ŽåããŠãŒãã£ãªãã£ã¯ããŒã¿ã¹ããªãŒã ã®è¡çªå·ãåºåããŸãã
$ sed '=' myfile
åºåè¡çªå·ã¹ããªãŒã ãšãã£ã¿ãŒã¯ãã³ã³ãã³ãã®åã«è¡çªå·ãå°å·ããŸããã
ãã®ã³ãã³ãã«ãã³ãã¬ãŒããæž¡ããsed
-n
ããŒã䜿çšãããšããã³ãã¬ãŒãã«å¯Ÿå¿ããè¡çªå·ã®ã¿ã衚瀺ãããŸãã
$ sed -n '/test/=' myfile
ãã¿ãŒã³ã«äžèŽããè¡çªå·ã衚瀺ãããã¡ã€ã«ããæ¿å
¥ãããããŒã¿ã®èªã¿åã
äžèšã§ã¯ãããŒã¿ãã¹ããªãŒã ã«æ¿å
¥ããæ¹æ³ã調ã¹ãsedãåŒã³åºããããšãã«äœãæ¿å
¥ããã¹ããã瀺ããŸããã ãã¡ã€ã«ãããŒã¿ãœãŒã¹ãšããŠäœ¿çšããããšãã§ããŸãã ãããè¡ãã«ã¯ã
r
ã³ãã³ãã䜿çšããŸããããã«ãããæå®ãããã¡ã€ã«ã®ããŒã¿ãã¹ããªãŒã ã«æ¿å
¥ã§ããŸãã åŒã³åºããšãã«ãè¡çªå·ãæå®ã§ããŸãããã®åŸããã¡ã€ã«ãŸãã¯ãã³ãã¬ãŒãã®å
容ãæ¿å
¥ããå¿
èŠããããŸãã
äŸãèããŠã¿ãŸãããïŒ
$ sed '3r newfile' myfile
ãã¡ã€ã«ã®å
容ãã¹ããªãŒã ã«è²Œãä»ããããã§ã¯ã
newfile
ãã¡ã€ã«ã®å
容ã
myfile
ãã¡ã€ã«ã®3è¡ç®ã®åŸã«æ¿å
¥ãããŸããã
r
ã³ãã³ããåŒã³åºããšãã«ãã³ãã¬ãŒãã䜿çšãããšã次ã®ããã«ãªããŸãã
$ sed '/test/r newfile' myfile
rã³ãã³ããåŒã³åºããšãã«ãã¿ãŒã³ã䜿çšãããã¡ã€ã«ã®å
容ã¯ããã¿ãŒã³ã«äžèŽããåè¡ã®åŸã«æ¿å
¥ãããŸãã
äŸ
ãã®ãããªã¿ã¹ã¯ãæ³åããŠãã ããã ããç¹å®ã®æåã·ãŒã±ã³ã¹ããããããèªäœã¯ç¡æå³ãªãã¡ã€ã«ããããŸãããããã¯å¥ã®ãã¡ã€ã«ããååŸããããŒã¿ã§çœ®ãæããå¿
èŠããããŸãã ããªãã¡ã
DATA
æåã®ã·ãŒã±ã³ã¹ã
newfile
圹å²ãæãã
newfile
ãšããŸãã
DATA
代ããã«äœ¿çšããã
DATA
ã¯ã
data
ãã¡ã€ã«ã«ä¿åãããŸãã
ãã®åé¡ã¯ãsedã¹ããªãŒã ãšãã£ã¿ãŒã®
r
ããã³
d
ã³ãã³ãã䜿çšããŠè§£æ±ºã§ããŸãã
$ Sed '/DATA>/ { r newfile d}' myfile
ãã¬ãŒã¹ãã«ããŒãå®éã®ããŒã¿ã«çœ®ãæããã芧ã®ãšããã
DATA
ãã¬ãŒã¹ãã«ããŒã®ä»£ããã«ãsedã¯
data
ãã¡ã€ã«ããåºåã¹ããªãŒã ã«2è¡ãè¿œå ããŸããã
ãŸãšã
ä»æ¥ã¯ãsedã¹ããªãŒãã³ã°ãšãã£ã¿ãŒã§ã®äœæ¥ã®åºæ¬ã«ã€ããŠèª¬æããŸããã å®éãsedã¯å€§ããªãããã¯ã§ãã ãã®ç 究ã¯ãæ°ããããã°ã©ãã³ã°èšèªã®ç 究ãšæ¯èŒã§ããŸãããåºæ¬ãç解ããã°ãå¿
èŠãªã¬ãã«ã§sedããã¹ã¿ãŒã§ããŸãã ãã®çµæãããã¹ããåŠçããèœåã¯æ³ååã«ãã£ãŠã®ã¿å¶éãããŸãã
ä»æ¥ã¯ä»¥äžã§ãã 次åã¯ãawkããŒã¿åŠçèšèªã«ã€ããŠèª¬æããŸãã
芪æãªãèªè
ïŒ æ¯æ¥ã®ä»äºã§sedã䜿çšããŠããŸããïŒ ãããããªããããªãã®çµéšãå
±æããŠãã ããã