Title:
1??????????4????????
2????
- ?????
- ?????????????
- ??????????(metacharacter)
- VIM???
- grep??
- sed???
- awk??
3?????
- ???????(regular expression, RE),
????????????,?????????????????????????????
4VIM??
5VIM???
- ????????????
- ?????????????i?
- ?????????????a?
- ?????????ESC?
- ????
- ?????????????wq
- ?????????????q!
- ??????????w
- ??????????w filename
6VIM????
- ??????????????2?g
- ?????????
- ????????
- ????????
- ??100???100g
7VIM????
- ???????/pattern/??,????pattern??????????
- ???????m,n/pattern/,????m???n???pattern?????
8grep??
9grep??
- ?Global search regular expression (RE) and Print
out the line??? - ?????????,???????????,???????
- ?????????????????,??????????,???????
10grep??
- grep option pattern input_files
11??grep????
-? ????,???????????? grep -2 grep test.txt??????????2?
-c --count,?????????,???????
-f file ??????????
-i --ignore-case,?????
-q --quiet,????
-l ????????????
-l ????????????
-n --line-number,?????????
-s --silent,???????
-v --revert-match,????????
-w --word-regexp,???\lt?\gt??,?????????????
12grep????????
?????? grep?????grep????
?????? grep?????grep????
. ????????????? gr.p??gr????????,??p??
??0???????? grep????0????????grep??
??????????? Ggrep????Grep?grep??
?????????????? A-FH-Zrep??????A-F?H-Z???????rep??
\( \) ?????? \(love\)?????love,?love????\1
\lt \gt ?????????????? \ltgrep???grep????? grep\gt???grep?????
x\5\, x\5,\,x\5,10\ x\5\, x\5,\,x\5,10\ x????5????????5?????????5-10?????
\w A-Za-z0-9 \WA-Za-z0-9 \w A-Za-z0-9 \WA-Za-z0-9 ??????(?)???????
\bgrep\b \bgrep\b ?????grep,???egrep?
13????????????
- ls l grep a
- grep test d
- grep test aa bb cc
- grep a-z\5\ aa
- grep w\(es\)t.\1 aa
- grep n datafile
- grep 4 datafile
- grep 5\.. datafile
- grep \.5 datafile
- grep wesn datafile
- grep A-ZA-Z A-Z datafile
- grep ss datafile
- grep a-z\9\ datafile
- grep \lta-z.n\gt datafile
14sed??
15sed??
- ???which sed????sed???
- ???????????,????????????????
- sed??????????????
- ?????????,???
- ??????????????????
16sed??
- ???????????????????,?????????????
- ???????????????,?????????????????,??????????
- sed option sed_command input_file
- sed option f sed_script_file input_file
- sed_script_file option input_file
17sed?????
-n Quiet, ????????,??????
-e ?????????,????????????
-f ???sed????
18sed???????
x ??,?????
x, y ??x-y????
xy ??x???,??y?
/pattern/ ?????????
x,y! ?????????x?y??
19??sed????
p ????? sed n 2,3p test.txt sed n /movie/p temp.txt
?????? sed e /music/ temp.txt
a\ ??????????? sed /jack/ \a here add new linep temp.txt
i\ ??????????? sed 4 \i here add new linep temp.txt
d ????? sed /music/d temp.txt
n ????????????,??????? sed /movie/n temp2.txt temp.txt
c\ ?????????? sed 4 \c i like it temp.txt
s ??????????? sed s/source/okstr/add before /p temp.txt
r ??????????? sed /name/r temp2.txt temp.txt
w ????????? sed s/name/my /w temp2.txt temp.txt
20????????????
sed s/\.//g temp.txt
sed e /abcd/d temp.txt
sed s/ / /g temp.txt
sed s///g temp.txt
sed s/\.//g temp.txt
sed //d temp.txt
sed s/.//g temp.txt
sed s/COL\(\)//g temp.txt
sed s/\///g temp.txt
21AWK??
22AWK??
- Linux??awk?????awk?nawk?gawk,?????awk??/bin/awk,?
??gawk (GNU awk) - awk???
- ?????????????????????
- ???????????
23awk???????
- awk option awk_script input_file1
input_file2 - ?awk_script????????!/bin/awk f
????,???????????,???shell??????????awk_script???a
wk_file input_file - ???awk_script???????????,???awk f awk_file
input_file(s)????
24awk?????
- ???????awk_cmd??,??awk_cmd?????NEWLINE??
- awk_cmd??????awk_pattern actions
- awk???????
- awk BEGIN actions awk_pattern1 actions
awk_patternN actions END actions
input_file - ??BEGIN ?END??????
25awk???????
- ????BEGIN??,?????actions
- ??????????,????????
- ???????????(FS/IFS)???????,???1,2,.,
?0??????? - ????????awk_pattern???,???????actions,????????????
???,????3,4,????? - awk????????????,??????????????,???????????
- awk?????????,?????????,??????
26awk_pattern???
- ????? /regexp/
- ???????? . ()
- ?awk??????,?????????????
- ???awk??????,????????0/1?
- ??awk /\0\.0-90-9./ input_file
- ????? ,gt, /regexp/,,
- ??
- awk 3 /d/ input_file
- awk '(1 lt 10 ) (2 gt 10) print "ok"'
input_file
27actions???
- actions??awk?????????
- actions?????????????,?????????
28awk??
- print 1 , 1
- printf?c???printf??
- next??????????
- nextfile????????????
- exitawk???????????END??,awk???END?actions
29awk??
- ????
- awk 'BEGIN x1 y3 xy print "x" x "
y" y - awk 'BEGIN x3 x2 yx2 print "x" x "
y" y ' - ??????
- ? if (condition) then-body else else-body
- ? while (condition) body
- ? do body while (condition)
- ? for (initialization condition increment)
body ?C???for??????? - ? break ??????for?while?do-while ??
- ? continue ??for?while?do-while???body?????,????
?????????
30awk????
- ??????
- NF??????????
- NR??????????????
- FNR???????????????
- FILENAME??????????
- ARGC???????,??????1
- ARGIND???????ARGV????
- ????1, 2, , NF, 0
- ??????
- FS ??????????(?????????)
- OFS ??????????(?????)
- OFMT ???????(??? .6g)
- RS ?????????(???NEWLINE) ORS
?????????(???NEWLINE) - ARGV ??????? ENVIRON ??????????????,??????????
???????? - ??cat /etc/passwd awk 'BEGIN FS"" print
"User name "1,"UID "4'
31awk?????
- ????varnamevalue
- ??????????????????,????????,????0?
- ??awk 'awk_script' awkvar1value1 awkvar2value2
.... input_file - ???awk_script??????????????????
- ??????shell??,????awk 'awk_script'
awkvar1shellvar1 awkvar2shellvar2 ....
input_file
32awk????
- ????
- int(x), sqrt(x), exp(x), log(x), sin(x), cos(x),
atan2(y,x), rand(), srand(x), srand() - ?????
- index(in, find), length(s), match(s, r),
sprintf(), sub(p,r,t), gsub(p,r,t), substr(str,
st, len), split(s,a,fs), tolower(str),
toupper(str) - ????
- close(filename), system(command)
33awk?????
- ???????
- function fun_name(param_list) function_body
- ??
- awk print sum, SquareSum(1, 2) function
SquareSum(x,y) sumxxyy return sum
grade.txt
34awk??
- ??????
- for ( element in array_name ) print
array_nameelement - ??
- awk BEGINprint split(123456789, mya, )
for ( i in mya ) print myai
35??awk?????
- ? ????awk_script???????
- ? ??awk_script?????????
- ? ????????????,??????????
- ? ?????????,?????????,?awk?????,???????
- ? ???????,???????????????(??????)
36- 2) ?awk?,???????????????,?????????????????????????
?????namen - 3) ???BEGIN??????????????,?????awk??????????????
- 4) awk??????????????????????????,?????????????????
????????????,????? awk_script??????(1?2?3...)??
????????????n(n???????NF)???? - 5) ????????????,??????????????????????????????????
awk??????????? \b ??? \t tab? \f ???? \ddd ????
\n ?? \r ??? \c ?????????eg \\??????
37awk?????printf
- ? ?? printf ("???????", ????)
- ? ?????????????,???????????????
- ? ?????????????????????,??????????????????????????
?????????????? - ? ????? -width.precfmt
????????????,????? - ??????????,???? width
????,?????????????,???? .prec
prec?????,??????????????????,???? fmt
??????,???????????,????? - ? ???fmt c ASCII??, d ?? ,e ????????, f ???,?
123.44 g ?awk???????????e?f ,o ????, s ???, x
????? - ? ??
- echo "65" awk ' printf ("c\n",0) ' // ??? A
- awk 'BEGINprintf ".4f\n",999' //??? 999.0000
- awk 'BEGINprintf "2 number8.4f8.2f",999,888'
// ??? 2 -
// number999.0000
888.000
38??
- sed?????????
- awk???????????
- col1 col2 col3 col4
- ???????????sed?awk????,???????????????perl/python?
??? - ??C/C?????????????,??,??1-2??????????????,?????
??