Wܪk(κ٬`Wܪk)OzL@ǯSrƦCAΥH jM/N/R @CΦhCrrA
²檺AWܪkNOΦbrꪺBzW@yܦzCWܪkäO@Ӥu{A
ӬO@ӦrBzзǨ̾ڡApGzQnHWܪk觋BzrANonϥΤ䴩Wܪku{~A
ou{ܦhAҦp vi, sed, awk C WܪktκzӻAbOܭnC]tη|ͫܦhTAoǰTnAȬOiA ɡAziHzLWܪk\ӱNnT^XӡAòͫKd\A²ƺzy{C~A ܦhM˳n]䴩WܪkRAҦplALo(LoUH)NOܭn@ӨҤlC ҥHAz̦nnAѥWܪkޯAbӺzDɡA~²Bzz`ưȡI G`ϥΪ̻ݭnh[mߡA]ثeܦhMOϥΥWܪkӹFyLoBRzتA FӥDzKQʡAϥΪ̦ܤ֭nݪWܪkNqI |
[root@test root]# grep [-acinv] 'jMr' filename ѼƻG -a GN binary ɮץH text ɮת觋jM -c Gp 'jMr' -i GjpgPAҥHjpgۦP -n GKX渹 -v GϦVܡAYܥXS 'jMr' e@I --color=auto iNT^ƦCXC dҡG [root@test root]# grep 'root' /var/log/secure N /var/log/secure oɮפ root @qX [root@test root]# grep -v 'root' /var/log/secure YӦS root ~NƨqXӨùWI [root@test root]# last | grep root YӦ榳 root ~NƨqXӨùWI |
[root@test root]# vi regular_express.txt "Open Source" is a good mechanism to develop programs. apple is my favorite food. Football game is not use feet only. this dress doesn't fit me. However, this dress is about $ 3183 dollars. GNU is free air not free beer. Her hair is very beauty. I cant finish the test. Oh! The soup taste good. motorcycle is cheap than car. This window is clear. the symbol '*' is represented as start. Oh! My god! The gd software is a library for drafting programs. You are the best is mean you are the no. 1. The world I like dog. google is the best tools for search keyword. goooooogle yes! go! go! Let's go. # I am VBird |
[root@test root]# LANG=en [root@test root]# export LANG |
[root@test root]# grep -n 'the' regular_express.txt 8:I can't finish the test. 12:the symbol '*' is represented as start. 15:You are the best is mean you are the no. 1. 16:The world |
[root@test root]# grep -vn 'the' regular_express.txt
|
[root@test root]# grep -in 'the' regular_express.txt 8:I can't finish the test. 9:Oh! The soup taste good. 12:the symbol '*' is represented as start. 14:The gd software is a library for drafting programs. 15:You are the best is mean you are the no. 1. 16:The world |
[root@test root]# grep -n 't[ae]st' regular_express.txt 8:I can't finish the test. 9:Oh! The soup taste good. |
[root@test root]# grep -n 'oo' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! The soup taste good. 18:google is the best tools for search keyword. 19:goooooogle yes! |
[root@test root]# grep -n '[^g]oo' regular_express.txt 2:apple is my favorite food. 3:Football game is not use feet only. 18:google is the best tools for search keyword. 19:goooooogle yes! |
[root@test root]# grep -n '[^a-z]oo' regular_express.txt 3:Football game is not use feet only. |
[root@test root]# grep -n '[0-9]' regular_express.txt 5:However, this dress is about $ 3183 dollars. 15:You are the best is mean you are the no. 1. |
[root@test root]# grep -n '^the' regular_express.txt 12:the symbol '*' is represented as start. |
[root@test root]# grep -n '^[a-z]' regular_express.txt 2:apple is my favorite food. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 12:the symbol '*' is represented as start. 18:google is the best tools for search keyword. 19:goooooogle yes! |
[root@test root]# grep -n '^[^a-zA-Z]' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 20:# I am VBird |
[root@test root]# grep -n '\.$' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 4:this dress doesn't fit me. 10:motorcycle is cheap than car. 11:This window is clear. 12:the symbol '*' is represented as start. 15:You are the best is mean you are the no. 1. 16:The world |
[root@test root]# cat -A regular_express.txt However, this dress is about $ 3183 dollars.^M$ |
[root@test root]# grep -n '^$' regular_express.txt
21:
|
[root@test root]# cat /etc/syslog.conf [root@test root]# grep -v '^$' /etc/syslog.conf | grep -v '^#' |
[root@test root]# grep -n 'g..d' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 9:Oh! The soup taste good. 16:The world |
[root@test root]# grep -n 'ooo*' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! The soup taste good. 18:google is the best tools for search keyword. 19:goooooogle yes! |
[root@test root]# grep -n 'goo*g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes! |
[root@test root]# grep -n 'g*g' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 3:Football game is not use feet only. 9:Oh! The soup taste good. 13:Oh! My god! 14:The gd software is a library for drafting programs. 16:The world |
[root@test root]# grep -n 'g.*g' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 14:The gd software is a library for drafting programs. 18:google is the best tools for search keyword. 19:goooooogle yes! |
[root@test root]# grep -n '[0-9][0-9]*' regular_express.txt 5:However, this dress is about $ 3183 dollars. 15:You are the best is mean you are the no. 1. |
[root@test root]# grep -n 'o\{2\}' regular_express.txt 1:"Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 9:Oh! The soup taste good. 18:google is the best tools for search keyword. 19:goooooogle yes! |
[root@test root]# grep -n 'go\{2,5\}g' regular_express.txt 18:google is the best tools for search keyword. |
[root@test root]# grep -n 'go\{2,\}g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes! |
RE r | NqPd |
^word | ݷjMr(word)b歺I |
dҡGgrep -n '^#' regular_express.txt jM歺 # }l@I | |
word$ | ݷjMr(word)bI |
dҡGgrep -n '!$' regular_express.txt N ! @CLXӡI | |
. | NyN@ӡzršA@wO@ӥNršI |
dҡGgrep -n 'e.e' regular_express.txt jMriHO (eve) (eae) (eee) (e e)A Ȧ (ee) IY e P e y@wzȦ@ӦrAӪťզr]OrI | |
\ | ršANSŸSNqhI |
dҡGgrep -n \' regular_express.txt jMt ' @I | |
* | ƹsөΦhӪe@ RE r |
dҡGgrep -n 'ess*' regular_express.txt Xt (es) (ess) (esss) rA`NA] * iHO 0 ӡAҥH es ]OŦXajMrCt~A] * ơye@ RE ršzŸA ]Ab * enۤ@ RE rųIҦpNrh y.*z I | |
\{n,m\} | s n m Ӫye@ RE ršz Y \{n\} hOs n Ӫe@ RE ršA YO \{n,\} hOs n ӥHWe@ RE ršI |
dҡGgrep -n 'go\{2,3\}g' regular_express.txt b g P g 2 Ө 3 Ӫ o sbrAY (goog)(gooog) | |
[] | rX RE SrŪŸ |
[list] dҡGgrep -n 'g[ld]' regular_express.txt jMt (gl) (gd) @ ݭnSOdNOAb [] yԥN@ӫݷjMrzA ҦpG a[afl]y NjMriHO aay, afy, aly Y [afl] N a f l NI [ch1-ch2] dҡGgrep -n '[0-9]' regular_express.txt jMtNƦr@IݯSOdNAbrX [] - OSNqALNӦrҦsrIoӳsP_P ASCII sXA ]AzsXݭn]wT(b bash AݭnTw LANG P LANGUAGE ܼƬO_TI) ҦpҦjgrh [A-Z] [^] dҡGgrep -n 'oo[^t]' regular_express.txt jMriHO (oog) (ood) O (oot) A ^ b [] ɡA NNqOyϦVܡzNҦpAڤnjgrAh [^A-Z] OAݭnSO`NOApGH grep -n [^A-Z] regular_express.txt ӷjMA oo{ɮפҦ泣QCXAH]o [^A-Z] OyDjgrzNA ]C@槡DjgrAҦpĤ@檺 "Open Source" N p,e,n,o.... pgrA H (") rAҥHMŦX [^A-Z] jMI |
RE r | NqPd |
+ | ơy@өΤ@ӥHWze@ RE r |
dҡGegrep -n 'go+d' regular_express.txt jM (god) (good) (goood)... rC o+ Ny@ӥHW o zҥHAW榨G|N 1, 9, 13 CXӡC | |
? | ysөΤ@ӡze@ RE r |
dҡGegrep -n 'go?d' regular_express.txt jM (gd) (god) oӦrC o? NyŪ 1 o zҥHAW榨G|N 13, 14 CXӡC So{AoӮר( 'go+d' P 'go?d' )GXP 'go*d' ۦPH QQݡAoOI ^_^ | |
| | Ω( or )觋XƭӦr |
dҡGegrep -n 'gd|good' regular_express.txt jM gd good oӦrA`NAOyΡzI ҥHA 1,9,14 oT泣iHQCLXӳIpGٷQnX dog OHNo˰ڡG egrep -n 'gd|good|dog' regular_express.txt | |
( ) | Xysաzr |
dҡGegrep -n 'g(la|oo)d' regular_express.txt jM (glad) (good) oӦrA] g P d OƪAҥHA ڴNiHN la P oo C ( ) AåH | Ӥj}ӡANiHաI ~Aoӥ\٥iHΨӧ@yhӭƸsաzPOI|ҨӻG echo 'AxyzxyzxyzxyzC' | egrep 'A(xyz)+C' WҤlANOAڭn}YO A O C A@ӥHW "xyz" rꪺN |
Name Chinese English Math Average DmTsai 80 60 92 77.33 VBird 75 55 80 70.00 Ken 60 90 70 73.33 |
[root@linux ~]# printf 'CL榡' ڤe ѼơG 榡譱XӯS˦G \a ĵinX \b ˰h(backspace) \f Mù (form feed) \n Xs@ \r Y Enter \t [tab] \v [tabl] \xNN NN ƪƦrAiHഫƦrrC C {yA`ܼƮ榡 %ns n OƦrA s N string AYh֭ӦrF %ni n OƦrA i N integer AYh־ƦơF %N.nf n P N OƦrA f N floating (BI)ApGpƦơA ]ڦ@nQӦơApIAY %10.2f oI dҡG dҤ@GNWYܦɮסAȦCXmWPZG( [tab] j [root@linux ~]# printf '%s\t %s\t %s\t %s\t %s\t \n' `cat printf.txt` Name Chinese English Math Average DmTsai 80 60 92 77.33 VBird 75 55 80 70.00 Ken 60 90 70 73.33 # ]ڱNWɮצs printf.txt ɮɦWAhiQΤWרҡA # NCӳrH [tab] j}CѤWXӬݡAMĤGHO OK A # OĤ@h]YdzrAҥHNLkFI %s ܥHr (string) # 觋Ӯi{ӤeCӨCӤehH \t Y [tab] ӹj}ڡI dҤGGNWzĤGHAOHrBơBpIܡG [root@linux ~]# printf '%10s %5i %5i %5i %8.2f \n' `cat printf.txt |\ > grep -v Name` DmTsai 80 60 92 77.33 VBird 75 55 80 70.00 Ken 60 90 70 73.33 # oӮɭԪXiNFIڱNXӤePƮ榡ӿXA # ̦쪺ӬO %8.2f oӶؤFIڥiHw藍PpƦƨӶi榡XA # ҦpܦUˤlɡAzۤvլݬݡA|OXGI # printf '%10s %5i %5i %5i %8.1f \n' `cat printf.txt | grep -v Name` dҤTGCXƭ 45 NrH [root@linux ~]# printf '\x45\n' E # oF]ܦnLiHNƭഫrApGz|g script ܡA # iHۦդ@UA 20~80 ƭȥNrOԣI ^_^ |
[root@linux ~]# sed [-nefr] [ʧ@] ѼơG -n GϥΦwR(silent)ҦCb@ sed ΪkAҦӦ STDIN Ƥ@볣|QCXùWCpG[W -n ѼƫAhugL sed SBz@(Ϊ̰ʧ@)~|QCXӡC -e GbOCҦWi sed ʧ@sF -f GN sed ʧ@gb@ɮפA -f filename hiH filename sed ʧ@F -r Gsed ʧ@䴩OWܪkykC(w]O¦Wܪkyk) -i GקŪɮפeAӤOѿùXC ʧ@G [n1[,n2]]function n1, n2 Go|sbA@Nyܶiʧ@ơzA|ҨӻApGڪʧ@ Oݭnb 10 20 椧i檺Ahy 10,20[ʧ@欰] z function UoǩNNG a GsWA a ᭱iHrAӳoǦr|bs@X{(ثeU@) c GNA c ᭱iHrAoǦriHN n1,n2 I d GRA]ORڡAҥH d ᭱q`NNF i GJA i ᭱iHrAӳoǦr|bs@X{(ثeW@)F p GCLAYNYӿܪƦLXCq` p |PѼ sed -n @_B@ s GNAiHiNu@Iq`o s ʧ@iHft WܪkIҦp 1,20s/old/new/g NOաI dҡG dҤ@GN /etc/passwd eCXAåBڻݭnCL渹APɡAбN 2~5 RI [root@linux ~]# nl /etc/passwd | sed '2,5d' 1 root:x:0:0:root:/root:/bin/bash 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown .....(᭱ٲ)..... # ݨFaH] 2-5 浃LRFAҥHܪƤANS 2-5 o # t~A`N@UA쥻ӬOnUF sed -e ~AS -e ]աI # Pɤ]n`NOA sed ᭱ʧ@AаȥH '' ӳAI # ӡApGunR 2 AiHϥ nl /etc/passwd | sed '2d' ӹFA # ܩ 3 ̫@AhO nl /etc/passwd | sed '3,$d' աI dҤGGӤWDAbĤG(YO[bĤT)[Wydrink tea?zrˡI [root@linux ~]# nl /etc/passwd | sed '2a drink tea' 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin drink tea 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin # KKIb a ᭱[WrNwNX{bĤG᭱oIpGOnbĤGeOH # nl /etc/passwd | sed '2i drink tea' NաI dҤTGbĤG᭱[JrAҦpyDrink tea or .....zydrink beer?z [root@linux ~]# nl /etc/passwd | sed '2a Drink tea or ......\ > drink beer ?' 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin Drink tea or ...... drink beer ? 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin # oӽdҪIOAڭ̥iHsWu@IiHsWnX # OC@椧nHϱu \ Ӷis檺W[IҥHAWҤlA # ڭ̥iHo{bĤ@檺̫᭱N \ sbաIO@wnI dҥ|GڷQN2-5檺eNyNo 2-5 numberzOH [root@linux ~]# nl /etc/passwd | sed '2,5c No 2-5 number' 1 root:x:0:0:root:/root:/bin/bash No 2-5 number 6 sync:x:5:0:sync:/sbin:/bin/sync # SF 2-5 AKKKKIڭ̭nƴNX{աI dҤGȦCX 5-7 [root@linux ~]# nl /etc/passwd | sed -n '5,7p' 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown # n[ -n ѼƩOHziHۦUF sed '5,7p' NDFI(5-7|ƿX) # S[W -n ѼƮɡAXƥiOtܦhI dҤGڭ̥iHϥ ifconfig ӦCX IP AYȭn eth0 IP ɡH [root@linux ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:51:FD:52:9A:CA inet addr:192.168.1.12 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::250:fcff:fe22:9acb/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 .....(HUٲ)..... # Aڭ̭nuO inet addr:..@ӤwAҥHoAQ grep P sed Ӯ [root@linux ~]# ifconfig eth0 | grep 'inet ' | sed 's/^.*addr://g' | \ > sed 's/Bcast.*$//g' # ziHNCӺu (|) L{}ӰAN|o]oI # hYhAN|oڭ̩һݭn IP YO 192.168.1.12 o dҤCGN /etc/man.config ɮתeA MAN ]wNXӡAneC [root@linux ~]# cat /etc/man.config | grep 'MAN'| sed 's/#.*$//g' | \ > sed '/^$/d' # C@AY # ܸӦ欰ѡAOn`NOAɭԡA # ѨäOgbĤ@ӦrAYOgbYӫOApUҼˡG # yshutdown -h now # oӬOOzA # NbOFC # ]Aڭ̤~|ϥΨN #.*$ oӥWܪkI dҤKGQ sed b ~/.bashrc ̫@[Jy# This is a testz [root@linux ~]# sed -i '$a # This is a test' ~/.bashrc # WY -i ѼƥiHA sed hק᭱ɮפeIӤOѿùXC # ܩ $a hN̫@~sWNC |
[root@linux ~]# awk '1{ʧ@1} 2{ʧ@2} ...' filename
|
[root@linux ~]# last
dmtsai pts/0 192.168.1.12 Mon Aug 22 09:40 still logged in
root tty1 Mon Aug 15 11:38 - 11:39 (00:01)
reboot system boot 2.6.11 Sun Aug 14 18:18 (7+15:41)
dmtsai pts/0 192.168.1.12 Fri Aug 12 12:07 - 12:08 (00:01)
|
[root@linux ~]# last | awk '{print $1 "\t" $3}'
dmtsai 192.168.1.12
root Mon
reboot boot
dmtsai 192.168.1.12
|
ܼƦW | NNq | NF | C@ ($0) ֦` | NR | ثe awk ҳBzOyĴXz | FS | ثejrAw]Oť |
[root@linux ~]# last | awk '{print $1 "\t lines: " NR "\t columes: " NF}'
dmtsai lines: 1 columes: 10
root lines: 2 columes: 9
reboot lines: 3 columes: 9
dmtsai lines: 4 columes: 10
|
B椸 | NNq |
> | j |
< | p |
>= | jε |
<= | pε |
== | |
!= |
[root@linux ~]# cat /etc/passwd | \ > awk '{FS=":"} $3 < 10 {print $1 "\t " $3}' root:x:0:0:root:/root:/bin/bash bin 1 daemon 2 ......(HUٲ)...... |
[root@linux ~]# cat /etc/passwd | \ > awk 'BEGIN {FS=":"} $3 < 10 {print $1 "\t " $3}' root 0 bin 1 daemon 2 ......(HUٲ)...... |
Name 1st 2nd 3th VBird 23000 24000 25000 DMTsai 21000 20000 23000 Bird2 43000 42000 41000 |
[root@linux ~]# cat pay.txt | \ > awk 'NR==1{printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total" } NR>=2{total = $2 + $3 + $4 printf "%10s %10d %10d %10d %10.2f\n", $1, $2, $3, $4, total}' Name 1st 2nd 3th Total VBird 23000 24000 25000 72000.00 DMTsai 21000 20000 23000 64000.00 Bird2 43000 42000 41000 126000.00 |
[root@linux ~]# cat pay.txt | \ > awk '{if(NR==1) printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total"} NR>=2{total = $2 + $3 + $4 printf "%10s %10d %10d %10d %10.2f\n", $1, $2, $3, $4, total}' |
[root@linux ~]# mkdir -p /tmp/test [root@linux ~]# cat /etc/passwd | \ > sed -e '4d' -e '6c no six line' > /tmp/test/passwd # `N@UA sed ᭱pGnWLӥHWʧ@ɡACӰʧ@eo[ -e ~I |
[root@linux ~]# diff [-bBi] from-file to-file ѼơG from-file G@ɦWA@lɮתɦWF to-file G@ɦWA@تɮתɦWF `NAfrom-file to-file iH - NA - NyStandard inputzNC -b G@AȦhӪťժt(Ҧp "about me" P "about me" ۦP -B Gťզ檺tC -i GjpgPC dҡG dҤ@G /tmp/test/passwd P /etc/passwd tG [root@linux ~]# diff /etc/passwd /tmp/test/passwd 4d3 <==o̬OAɮ(/etc/passwd)ĥ|QR (d) < adm:x:3:4:adm:/var/adm:/sbin/nologin 6c5 <==o̬OAɮתĤQNkɮ(/tmp/test/passwd)Ĥ < sync:x:5:0:sync:/sbin:/bin/sync --- > no six line # oaI diff Nڭ̭𫍧Bz粒FI |
[root@linux ~]# diff /etc /tmp/test ......(eٲ)..... Only in /etc: paper.config diff /etc/passwd /tmp/test/passwd 4d3 < adm:x:3:4:adm:/var/adm:/sbin/nologin 6c5 < sync:x:5:0:sync:/sbin:/bin/sync --- > no six line Only in /etc: passwd- ......(᭱ٲ)..... |
[root@linux ~]# cmp [-s] file1 file2 ѼơG -s GNҦPI줸BCXӡC] cmp w]ȷ|XĤ@ӵo{PIC dҡG dҤ@G cmp @U /etc/passwd P /tmp/test/passwd [root@linux ~]# cmp /etc/passwd /tmp/test/passwd /etc/passwd /tmp/test/passwd differ: byte 106, line 4 |
[root@linux ~]# mkdir /tmp/old; cp /etc/passwd /tmp/old [root@linux ~]# mkdir /tmp/new; cp /tmp/test/passwd /tmp/new [root@linux ~]# cd /tmp ; diff -Naur old/ new/ > test.patch |
[root@linux ~]# patch -pN < patch_file ѼơG -p G᭱iHyXhؿzNC dҡG dҤ@GNs@XӪ patch file Ψӧsª [root@linux ~]# cd /tmp/old [root@linux ~]# patch -p1 < /tmp/test.patch patching file passwd # o̷|ϥ -p1 OH]ڭ̦bsªƮɡAOb /tmp UA # ӹڪƬOb /tmp/old ̭A]Aڭ̶iJ /tmp/old ɡA # Ad\ /tmp/test.patch Ĥ@pUG # diff -Naur old/passwd new/passwd ( head -n 1 /tmp/test.patch) # o{Aڭ̩ҦbؿO old ̭AҥHANnh@hؿC |
[root@linux ~]# pr /etc/man.config 2003-02-10 23:20 /etc/man.config Page 1 # # Generated automatically from man.conf.in by the # configure script. .....HUٲ...... |