EX½s¿è¾¹

Introduction

Ex Instruction Format

§@¥Î«ü¥O

Argument

©I¥sUNIX«ü¥O

Regular Expression

Address Arithmetic

Ex Script

Ex Examples

Ex Examples

Ex Examples

Ex Examples

Ex Examples


Untitled Document
Introduction
   
¦bvi¤¤¡A¥´¤@­Ó«_¸¹ ':' ¡A´N¥i¥H¶i¤JexºN¦¡¡A vi µ²¦Xex«á¡A¨ä«Â¤OÅܱo±j ¤j±oÃø¥H·Q¹³¡C
   
ex¬O¤@­Ó¥H ¡u¦æ¡v ¬°§@¥Î³æ¦ìªº½s¿è¾¹¡A
   
¥i¥H¦P®É¹ï«Ü¦h¦æ¤U¦P¤@«ü¥O¡A¦]¦¹¥i¥H¦P®É¶¡§@³\¦h¨Æ¡C
   
¨Ò¦p¤@­Ó²³æªº«ü¥O¥i§â¤@½g¤å³¹¤¤¤§©Ò¦³ "nccu" ¦r¦ê§ï¦¨ "NCCU" ¦r¦ê¡C
  
³oºØ¯à¤O¦b«Ü¦h½s¿è¾¹¤¤³£¦³¡C ¦Ó¥B¡AŪªÌ¤]³\·|½èºÃ³oºØ±¡ªp¥X²{ªº±¡§Î¤£¦h¡A¦n³B¤£¤j¡C
¦ý¡A
 
ex ¾Ö¦³¤@«Â¤O±j¤jªº "Regular Expression"
   
¥Ñ©ó¨ã³Æ¤FRegular Expressionªº¯à¤O¡Aex¥i¥Î ¦P¤@«ü¥O¦P®É³B²z³\¦h¤£¦Pªºª«¥ó¡A
   
¨Ò¦p¡A¥i¦P®É§â©Ò¦³ "nccu"¡B"Nccu" µ¥¤@°_§ï¦¨ "NCCU"¡A
   
©Î¬O§â§t¦³¬Y¤@¦r¦êªº¡u¦æ¡v¦P®É§R°£¡C
 
µ§ªÌ¦³¦¹Ä´³ë¡G
   
¦pªG§âvi¬Ý¦¨¤@¯ëªº¯¥¼u¡A¨ºvi¥[ex´N¦¨¤F­¸¼u¡A¦p¦A¥[¤WRegular Expression¡A¨º´N¦¨¤F±a®Ö¤l¼uÀYªº¬w»Ú­¸¼u¤F¡C
  
ÁöµM¨ä¥¦½s¿è¾¹¤]¥i¯à¦³Ãþ¦üªº¥\¯à¡]¨Ò¦pword¡^¡A ¦ý¨Ï¥Îex¤¤ªºRegular Expression¦³¤@­Óµ´¤jªº¦n³B¡A¦]¬° ex¤¤ªºRegular Expression»Psed¡Bawk¡Blex¡Bperlµ¥ UNIX±`¥Î³nÅé¤u¨ã©Ò¨Ï¥ÎªÌ¤j­P¬Û¦P¡A¦]¦¹±`¨Ï¥ÎexªÌ¡A ¦ÛµM¦ÓµM·|§âRegular Expression½m±o¦n¦nªº¡A¦b¨Ï¥Î ¨ä¥L³nÅé¤u¨ã®É¤j¤jªº¦û«K©y¡C
(¦pªG¥Îªº¬O¤£¦PRegular Expressionªº½s¿è¾¹¡A¨º ´N¨S¾÷·|±`±`½m²ßUNIX¤¤³Ì³q¥ÎªºRegular Expression¤F¡C) ŪªÌ¤]³\·|»{¬°¡G·|¨Ï¥ÎRegular Expression»P§_¡A¹ï¨Ï¥ÎªÌ ªº¼vÅT¨Ã¤£¤j¡C¨ä¹ê¨Ìµ§ªÌ¦Û¤vªº¸gÅç¦Ó¨¥¡A¦pªG¤£·| Regular Expressionªº¸Ü¡Aµ§ªÌ¦bUNIX¤W°µ¨Æªº¥Í²£¤O·|³Qíu±¼¤E¦¨¥H¤W¡C
Thu Oct 19 10:55:22 CST 2023 Untitled Document
Ex Instruction Format
 
Exªº«ü¥O¤À¦¨¤T¬q:
§@¥Î½d³ò  §@¥Î«ü¥O  Argument
§@¥Î½d³ò
   
¡u§@¥Î½d³ò¡v«üªº¬O§@¥Î«ü¥O©Ò­n§@¥Îªº¡u¦æ¡v¡A¨ä®æ¦¡¦p¤U:
®æ¦¡ §@¥Î½d³ò
n
²Än¦æ
m,n
²Äm¦æ¨ì²Än¦æ¤§¶¡ªº°Ï¶ô
/pattern/
¥»¦æ¶}©l¨ì³Ì«á¤@¦æ¤§¶¡²Ä¤@­Ó§t¦³pattern¦r¦êªº¡u¦æ¡v
g/pattern/
©Ò¦³§t¦³ pattern ¦r¦êªº¡u¦æ¡v
v/pattern/
©Ò¦³¤£§t pattern ¦r¦êªº¡u¦æ¡v
¥yÂI '. '
¥»¦æ
$
³Ì«á¤@¦æ
 
¥Ñ©ó¨Ï¥ÎªÌ¦b¤U«ü¥O®É¨Ã¤£¤@©wª¾¹D³Ì«á¤@¦æªº¦æ¼Æ¡A¦³¨Ç®É­Ô«D±`¤£¤è«K¡C
   
¨Ò¦p¡G§A·Q­n±N©Ò¦³ªº "nccu" ¦r¦ê§ï¦¨ "NCCU" ¦r¦ê¡A¦Ó°²¦p³Ì«á¤@¦æ¬O²Ä1024 ¦æ¡A¤U­±ªº«ü¥O¥i¥H°µ¨ì¡G
   1,1024s/nccu/NCCU/g
   
¦pªG§A¤£ª¾¹D³Ì«á¤@¦æ¬O1024¦æ¡A¨º´N«Ü¤£¤è«K¤F¡C
   
¦bex¤¤¡A'$' ¦r¤¸¥i¥Nªí³Ì«á¤@¦æ¡C
   1,$s/nccu/NCCU/g
   
·í¤@­Óex«ü¥O¨S¦³«ü©w§@¥Î½d³ò®É¡A¨º¥i¯àªí¥Ü¸Ó«ü¥O±N§@¥Î©ó¥þ³¡½d³ò¡A ©Î¬O¥»¦æ¡Aµø«ü¥O¤§¤º®e¦Ó©w¡C
   
¦p: «ü¥Od¬O­n§R°£¥»¦æ¡A
   
¦Ó«ü¥Ow«o­n±N¾ã­ÓÀɮצsÀÉ¡C
Thu Oct 19 10:55:22 CST 2023 Untitled Document
§@¥Î«ü¥O
   
±`¥Îªº«ü¥O¥]¬Ad¡Ap¡Amo¡Aco¡Aw¡Ar¡Asµ¥«ü¥O
«ü¥O §@¥Î ¨Ò »¡©ú
d
§R°£
1,100d
§R°£1¦Ü100¦æ
p
¦L¥X©ó¿Ã¹õ¤W
2,10p
2¦Ü10¦æ¦L¦Ü¿Ã¹õ¤W
s
¥N´«¦r¦ê
1,$s/nccu/NCCU/g
©Ò¦³nccu¦r¦ê§ï¦¨NCCU
'&' ªº²Å¸¹¥Nªíreuse«e­±pattern¤ñ¹ï¨ìªº³¡¤À (½Ð°Ñ¦Ò Regular Expression ³¹¸`)
¤À¹j²Å¸¹¤£¶·©T©w¬°'/'¡A¥i¬°¥ô·N¦r¤
mo
·h°Ê°Ï¶ô
10,20mo30
10¦æ¦Ü20¦æ°Ï¶ô²¾¦Ü30¦æ¤§«á
co
½Æ»s°Ï¶ô
10,30co30
10¦æ¦Ü20¦æ°Ï¶ô½Æ»s¦Ü30¦æ¤§«á
r
Ū¶i¨ä¥LÀÉ®×
10r in_file
Ū¶iin_fileÀɮסA¸m©ó¥»¦æ¤§«á
w
¼g¨ìÀÉ®×
10,20w out_file
10¦æ¦Ü20¦æ°Ï¶ô¼g¦Üout_fileÀÉ®×
q
¸õ¥Xvi©Îex
q
¸õ¥Xvi
Thu Oct 19 10:55:22 CST 2023 Untitled Document
Argument
   
©Ò¿×ªºargument¡A§Y¬O°Ñ¼Æ¤§·N¡A¦]¬°¦³®É­Ô¥¦ªº§t·N¨Ã«D°Ñ¼Æ¡A¦]¦¹¦¹¦aÁÙ¬O ¥Îargument¥HÁ×§K²V²c¡C
   
±q¤W­±ªº¨Ò¤l¤¤¥i¬Ý¥X¡A°£¤F«ü¥Os¤§¥~¡A¨ä¥L´X­Ó«ü¥O³£«Ü²³æ¡A«Ü®e©ö²z¸Ñ¡C
   
¦Ó«ü¥Os«h¦³«Ü¦hÅܤơA¥Îªº¥©ªº¸Ü¡A«Â¤OµL½a¡C§Ú­Ì´N¨Ó¬Ý¬Ý¥LªºÅܤơC
 
¤À¹j²Å¸¹
   
¦b«ü¥Os¤§«á¡A¦³¤G¦Ü¤T­Ó¦r¦ê¡A¥H¤À¹j²Å¸¹¤À¶}¦¨¤G­Ó°Ï¶¡¡A
   
³Ì±`¥Îªº¤À¹j²Å¸¹¬O'/'¡A
   
¦ý¬O¡A¥u­n¤T­Ó²Å¸¹©¼¦¹¬Û¦P¡A¥Î¥ô·Nªº²Å¸¹³£¥i¥H¡A
   
¤U¨Ò¤¤¨â­Ó«ü¥O¨ä¹ê¬O¬Û¦Pªº¡C
   1,100s/nccu/NCCU/g
   1,100s|nccu|NCCU|g
   
²Ä¤@­Ó°Ï¶¡¥Nªí³Q´À´«ªº¦r¦ê¡A
²Ä¤G­Ó°Ï¶¡¥Nªí±ý¥N¶i¥hªº¦r¦ê¡A
²Ä¤G­Ó°Ï¶¡¤§«á¬O©Ò¿×ªºpost modifier¡A»²§U«ü¥O¡C
   
±`¥Îªº»²§U«ü¥O¥u¦³¤@­Óg¡C
   
¨Ò¡G
­ì¤å «ü¥O µ²ªG
karama vs. karama
s/karama/KARAMA/g
KARAMA vs. KARAMA
karama vs. karama
s/karama/KARAMA/
KARAMA vs. karama
   
²Ä¤@­Ó«ü¥O·|§â¥»¦æ¤¤©Ò¦³karama¦r¦ê§ï¬°KARAMA¡A
   
¦Ó«áªÌ¥u·|§ï²Ä¤@­Ókarama¦r¦ê¡C
Thu Oct 19 10:55:22 CST 2023 Untitled Document
©I¥sUNIX«ü¥O
sh
Execute a subshell, back to vi by `^D'.
[x,y]!<cmd>
Execute a shell <cmd> [on lines x through y; these lines will serve as input for <cmd> and will be replaced by its standard output].
[x,y]!! [<args>]
Repeat last shell command [and append <args>].
[x,y]!<cmd> ! [<args>]
Use the previous command (the second `!') in a new command.
[*]!<move><cmd>
The shell executes <cmd>, with as standard input the lines described by <*><move>, next the standard output replaces those lines (think of `cb', `sort', `nroff', etc.).
[*]!!<cmd>
Give <*> lines as standard input to the shell <cmd>, next let the standard output replace those lines.
[*]!!! [<args>]
Use the previous <cmd> [and append <args> to it].
x,y w !<cmd>
Let lines x to y be standard input for <cmd> (notice the <sp> between the `w' and the `!').
r!<cmd>
Put the output of <cmd> onto a new line.
Thu Oct 19 10:55:23 CST 2023 Untitled Document
Regular Expression
Regular Expression
   
¦pªG¤j®a¤w¸g²ßºD¤F¨Ï¥Î¸U¥Î¦r¤¸©óÀɮצWºÙªº±Ô­z¡AÀ³¸Ó¤w¸g ¨É¨ü¨ì¨ä¼Ö½ì¡G¥u­n¥Î¤@­Ó²Å¸¹´N¥i¥H¥Nªí«Ü¦h¤£¦Pªº¦r¦ê¡A¥i¥H ¬Ù¤U«Ü¦h¥\¤Ò¡A¤j´T´£°ª®Ä²v¡C ¦P¼Ëªº¡A¦bex¤¤¡A¤]¤ä´©¦r¦êªº Regular Expression. ·í§Ú­Ì¦b·j´M¦r¦ê¡A©Î¤ñ¹ï¦r¦ê®É¡A¦pªG¹ï¶H¬O¤@¸sÃþ¦üªº¦r¦ê¡A ¦pªG¯à¥Î¤@¨Ç²Å¸¹¨Ó¥Nªí¡A±N·|«Ü¤è«K¡C°²³]§Ú­Ì­n±N 'shell' ¤Î 'Shell' ¥þ³¡§ï¦¨ 'SHELL'¡A¦pªG¨S¦³Regular Expression, ¨º§Ú­Ì´N­n¤U¨â¦¸´À¥N«ü¥O¤~¦æ¡A¦ý¦³¤FRegular Expressionªº¸Ü¡A §Ú­Ì¥u»Ý¤@­Ó´À¥N«ü¥O§Y¥i¡A¦p¤U¡F
  1,$s/[Ss]hell/SHELL/g
ŪªÌÀ³¸Ó¥i¥H²q¥X¨Ó³o¸Ìªº'[]'©Ò ¥Nªíªº·N¸q¡A¸òÀɮצWºÙªº¸U¥Î¦r¤¸¤¤ªº'[]'¬O¤@¼Ëªº¡C ¥H¤U¬O ex ¤ä´©ªº Regular Expression:
¡D
¥Nªí¥ô·N¦r¤¸
¡¯
¥Nªí«e­±ªº¦r¤¸¥X²{¥ô·N¦h¦¸¡]¥]¬A¹s¦¸¡^
¡Ï
¥Nªí«e­±ªº¦r¤¸¥X²{¤@¦¸©Î¤@¦¸¥H¤W
[abcd]
¥Nªía©Îb©Îc©Îd
[a-d]
¥Nªía©Îb©Îc©Îd
[^a]
¥Nªí¤£¬Oaªº¥ô·N¦r¤¸
[^a-d]
¥Nªí¤£¬Oa©Îb©Îc©Îdªº¥ô·N¦r¤¸
^
¥Nªí¤@¦æ¦r¦êªº¶}ÀY
$
¥Nªí¤@¦æ¦r¦êªºµ²§À

 
Back Reference

&
¥Nªí¾ã­Ó¤ñ¹ï¨ìªº Pattern
\d
²Ä d ­Ó¤ñ¹ï¨ìªº¦¸¦r¦ê (substring)
   
Back Reference ªº¾÷¨î¹ï©ó¤ä´© Regular Expression ªº´À¥N«ü¥O ¬O«D±`­«­nªº¡A¦]¬°¯à²Å¦XRegular Expression ªº¦r¦ê¥i¯à¤£¤î¤@ºØ¡A´À¥Nªº¦r¦ê¦pªG»Ý­n¨Ï¥Î¨ì©Ò¤ñ¹ï¨ìªº¦r¦ê¡A (¨Ò¦p±N¤ñ¹ï¨ìªº¦r¦ê¥[¤W¬A©·)¡A¦pªG¨S¦³ Back Reference ¾÷¨î¡A ¡A®Ú¥»¼g¤£¥X¨Ó©Ò»Ýªº´À¥N«ü¥O¡C
 
¨Ò
 1,$s/NCCU/(&)/g    #±N©Ò¦³ªºNCCU ¥[¤W¬A©·
 1,$s/[Nn][Cc][Cc][Uu]/(&)/g    #±N©Ò¦³¤j¤p¼gªºNCCU ¥[¤W¬A©·
 1,$s/..*/"&"/   #±N¨C¤@¦æ¤§«e«á¥[¤W¤Þ¸¹  
 1,$s/\(..*\)  *\(..*\) /\2 \1 /     # ±N¥HªÅ¥Õ¤À¹jªº«e¨â­ÓÄæ¦ì¤ºªº¦r¦ê¥æ´« (swap 1st and 2nd words) 
Regular Expression Web Page Thu Oct 19 10:55:23 CST 2023 Untitled Document
Address Arithmetic
   
¥H¤U¬O¤@¨Ç¨Ò¤l±Ô©ú©Ò±ý§@¥Îªº½d³ò¡A(±q­þ¤@¦æ¦Ü­þ¤@¦æ)¡G
$-1
³Ì«á²Ä¤G¤@¦æ
.-5,.+5
¤W­±5¦æ¦Ü¤U­±5¦æ
.-5,.5
¦P¤W
-,.
previous and current lines
Thu Oct 19 10:55:23 CST 2023 Untitled Document
Ex Script
   
§Ú­Ì¥i¥H±N ex ªº«ü¥O©ñ¦b¤@­ÓÀɮפº¡AµM«á§Q¥Î I/O Âà¦V ½Ð ex ±qÀɮפ¤Åª¨ú«ü¥O¶i¦æ½s¿è¦p¤U¡G
 
  ex file < ex.script
³o¬O«D±`¦³¥Îªº¡C¦pªG §Ú­Ì­n¹ï«Ü¦hÀÉ®×¶i¦æ¬Û¦Pªº½s¿è°Ê§@¡A­YµL¦¹ºØ¥\¯à¡A§Ú­Ì ¥u¦n¥Î¤@­Ó½s¿è¾¹±NÀɮפ@­Ó­Ó¥s¥X¨Ó½s¿è¡A¨º¬O«D±`µh­Wªº¨Æ¡A ¦³¤F³oºØ¥\¯à¡A§Ú­Ì¥u»Ý§Q¥Î½s¿è¾¹»s³y¤@­Óex «ü¥OÀÉ¡AµM«á ¼g¤@­Óshell script ±N©Ò¦³Àɮק妸½s¿è¦p¤U:
for i in *.htm
do
   ex $i < ex.script
done
Thu Oct 19 10:55:23 CST 2023 Untitled Document
Ex Examples
 
¥H¤U¬O§ó¦hªºex ¨Ò¤l¡G
1.$p
print the extire file
s/abc/def/
change the first 'abc' to 'def' in the current line .TR s/abc/def/g | change all 'abc' to 'def' in the current line
1,$s/abc/def/g
change all 'abc' to 'def' in all lines
/x.y/
search x+y, x-y, x.y, etc.
.s/././
convert the first character to '.' in the current line
s/\./,/g
convert alll '.' into ',' in the current line
s/\\\.//
convert first \x\.\y into \x\y in the current line ->
s/x../x/
same as above ->
s/..y/y/
same as above ->
s:/::g
remove all '/' in the current line
s/$/ time/
append ' time' to the end of line
s/^/ /
add a space to the beginning of line
1,$s/^[0-9]*//
remove all numbers that are at the benninging of the line
/^[^0-9]/
search for the lines that do not begin with a number
?^[^ ]?
search backward for the lines that do not begin with a space
/^[^^]/
search for the lines that do not begin with a '^'
g/^$/d
delete all blank lines
g/^[ ]*$/d
same as above but allow space and tab
Thu Oct 19 10:55:23 CST 2023 Untitled Document
Ex Examples
«ü¥O ­ì¦r¦ê ·s¦r¦ê
s/x *y/x y/
x     y
x y
s/x-*y/x y/
x------y
x y
s/x\.*y/x y/
x......y
x y

«ü¥O ·N¸q
s/the/& best/
change 'the' to 'the best'
s/..*/(&)/
parenthesize a line
1,$j
join all lines into one big line
Thu Oct 19 10:55:24 CST 2023 Untitled Document
Ex Examples
/a/,/b/p
print from the line containing 'a' to the line containing 'b'
/a/;/b/p
same as above, but must be in different lines
/^\.AB/,/^\.AE/w abstract
write the abstract of a troff document to file abstract

g/^\./p
print all lines started with a '.'
v/^\./d
delete all lines not started with a '.'
g/^0/s/$/<br>/
append <br> to all lines started with a '0'
20,30g/^0/s/$/<br>/
same as above but within lines 20 to 30
g/^$/d
delete all blank lines
Thu Oct 19 10:55:24 CST 2023 Untitled Document
Ex Examples

10,20mo30
move line 10-20 to line 30
10,20co30
copy line 10-20 to line 30
1,$t$
duplicate entire file

   
·íÀɮ׫ܤj¡A¦Ó line number «D±`ªø¡A ´N«Ü¤£¤è«K¡A¨Ò¦p
23414,45326mo65432
   
¥i¥H¦bvi ¤º¥Î'mx' ±Ncurrent line ¼Ð°O°_¨Ó (x ¬O¼Ð°Oªº¦r¥À)¡A¨Ò¦p¤W¨Ò¡A¥i¦b
line 23414 ¼Ð°O a (ma)¡A
line 45326 ¼Ð°O b (mb)¡A
line 65432 ¼Ð°O c (mc)¡A
µM«á§Q¥Î¤U­±«ü¥O¨ú¥N­ì«ü¥O¡C
'a,'bmo'c
Thu Oct 19 10:55:24 CST 2023 Untitled Document
Ex Examples

!who
escape to shell and run 'who'
r! who
escape to shell and run 'who' and get the output back
1,$!sort
sort the entire file
Thu Oct 19 10:55:24 CST 2023