正則表達式元字符:
^ #錨定行的開始。如:/^sed/ 匹配所有以sed開頭的行。 $ #錨定行的結束。如:/sed$/ 匹配所有以sed結尾的行。 . #匹配一個非換行字符。 如:/s..d/ 匹配s後接任意兩個個字符,最後是d。 * #匹配零個或多個字符。 如:/*sed/ 匹配所有模板是一個或多個空格後緊跟sed的行。 [] #匹配一個指定範圍內的字符。 如/[Ss]ed/ 匹配Sed和sed。 [x-y] #匹配指定範圍內的一個字符。 如 /[a-z]ed/ 匹配一個a-z之間任意字符後跟ed的行。 [^] #匹配一個不在指定範圍內的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一個字母開頭,緊跟ed的行。 \ #用來轉義字符。 如: /sed\./匹配包含sed後面跟一個句點. (未經轉義的句點通常匹配單個字符) \< #詞首定位符。 如:/\<love/匹配包含以love開頭的單詞的行。 \> #詞尾定位符。 如/love\>/匹配包含以love結尾的單詞的行。 \(..\) #匹配稍後將要使用的字符的標籤。 如s/\(love\)able/\1rs,loveable被替換成lovers。 #最多可以使用9個標籤,模式中最左邊的標籤是第1個。用\1表示。 x\{m\} #重複字符x,m次,如:/0\{5\}/匹配包含5個o的行。 x\{m,\} #重複字符x,至少m次,如:/o\{5,\}/匹配至少有5個o的行。 x\{m,n\} #重複字符x,至少m次,不多於n次,如:/o\{5,10\}/匹配5--10個o的行。 & #保存搜索字符用來替換其他字符,如s/love/**&**/,love這成**love**。
Postfix方括號字符集:
[:alnum:] #數字字符 [:lower:] #小寫字母字符 [:alpha:] #字母字符 [:print:] #可顯示的字符 [:blank:] #空格(space)與定位符(tab)字符 [:punct:] #標點符號字符 [:cntrl:] #控制字符 [:space:] #空白(whitespace)字符 [:digit:] #數字字符 [:upper:] #大寫字母字符 [:graph:] #非空格字符 [:xdigit] #十六進制數字
Sed學習:
1.sed選項:
-e command, --expression=command #允許多項編輯。 -f, --filer=script-file #指定sed腳本文件名。 -n, --quiet, --silent #取消默認的輸出。 -V, --version #打印版本和版權信息。 -h --help #打印幫助。
2.sed命令:
a\ #在當前行後添加一行或多行 c\ #用新文本修改(替換)當前行中的文本 d #刪除行 i\ #在當前行之前插入文本 h #把模式空間裏面的內容複製到暫緩衝區 H #把模式空間裏面的內容追加到暫緩衝區 g #取出暫存緩衝區的內容,將其複製到模式空間,覆蓋該處原有內容。 G #取出暫存緩衝區的內容,將其複製到模式空間,追加在原有內容後面。 l #列出非打印字符。 p #打印行。 n #讀入下一輸入行,並從下一條命令而不是第一條命令開始對其的處理。 q #結束或退出sed。 r #從文件中讀取輸入行。 ! #對所選行以外的所有行應用命令。 s #用一個字符串替換另一個。
3.sed替換標誌:
g #在行內進行全局替換。 p #打印行。 w #將行寫入文件。 x #交換暫存緩衝區與模式空間的內容。 y #將字符轉換爲另一字符(不能對正則表達式使用y命令)
sed示例:
[root@localhost mnt]# cat example.txt one line text two line text three line text four line text five line text end line !!!
刪除:d命令:
1.#刪除文件中的第二行 [root@localhost mnt]# sed '2d' example.txt one line text three line text four line text five line text end line !!! 2.#刪除文件中的最後一行 [root@localhost mnt]# sed '$d' example.txt one line text two line text three line text four line text five line text 3.#刪除第三行到末尾所有行 [root@localhost mnt]# sed '3,$d' example.txt one line text two line text 4.#刪除文件中包含text的行 [root@localhost mnt]# sed '/text/'d example.txt end line !!!
替換:s命令:
1.#在整行範圍內把e替換爲E。如果沒有g標記,則每行只匹配第一個e。 [root@localhost mnt]# sed 's/e/E/g' example.txt onE linE tExt two linE tExt thrEE linE tExt four linE tExt fivE linE tExt End linE !!! 2.#(-n)選項和p標誌一起使用表示只打印那些發生替換的行。 [root@localhost mnt]# sed -n 's/d/D/p' example.txt enD line !!! 3.#&符號表示替換換字符串中被找到的部份。 [root@localhost mnt]# sed -n 's/^end/&for/p' example.txt endfor line !!! #這裏&替換成爲end 4.#on標記爲1,所有的one都會被替換爲oncoming! [root@localhost mnt]# sed -n 's/\(on\)e/\1coming/p' example.txt oncoming line text 5.#不論什麼字符,緊跟着s命令的都被認爲是新的分隔符,所以這裏的#是分隔符; [root@localhost mnt]# sed -n 's#one#Haha#p' example.txt Haha line text
選定行的範圍:逗號
1.#所有在模板th和f所確定的範圍內的行都被打印。 [root@localhost mnt]# sed -n '/th/,/f/p' example.txt three line text four line text 2.#打印從第二行到第一個包含以four開始的行之間的所有行。 [root@localhost mnt]# sed -n '2,/^four/p' example.txt two line text three line text four line text 3.#對於模板one和three之間的行,每行的行首添加字符串Ceshi。 [root@localhost mnt]# sed '/one/,/three/s/^/Ceshi/' example.txt Ceshione line text Ceshitwo line text Ceshithree line text four line text five line text end line !!!
多點編輯:e命令
1.#(e)選項允許在同一行裏執行多條命令。 [root@localhost mnt]# sed -e '1,3d' -e 's/five/FFFF/' example.txt four line text FFFF line text end line !!! 2.#一個比-e更好的命令是--expression。它能給sed表達式賦值。 [root@localhost mnt]# sed --expression='s/one/ONE/' --expression='/h/d' example.txt ONE line text two line text four line text five line text end line !!!
從文件讀入:r命令
1.#hosts文件內容被讀進來,顯示在與one匹配的行後面。 #如果匹配了多行,那麼hosts內容將顯示在所有的匹配行下面。 [root@localhost mnt]# sed '/one/r /etc/hosts' example.txt one line text 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 two line text three line text four line text five line text end line !!!
寫入文件:w命令
1.#example中所有包含f的行都被寫入到write.txt中。 [root@localhost mnt]# sed -n '/f/w write.txt' example.txt [root@localhost mnt]# cat write.txt four line text five line text
追加命令:a命令
1.#“----->this is a test”被追加到匹配three的行後面,sed要求命令a後面有一個反斜槓。 [root@localhost mnt]# sed '/three/a\----->this is a test' example.txt one line text two line text three line text ----->this is a test four line text five line text end line !!!
插入:i命令
1.#“----->this is a new line”被插入到匹配three的行前面,sed要求命令i後面有一個反斜槓。 [root@localhost mnt]# sed '/three/i\----->this is a new line' example.txt one line text two line text ----->this is a new line three line text four line text five line text end line !!!
下一個:n命令
1.#如果three被匹配,則移動到匹配行的下一行,替換這一行的line爲LINE,並打印且繼續。 [root@localhost mnt]# sed '/three/{n; s/line/LINE/; }' example.txt one line text two line text three line text four LINE text five line text end line !!!
變形:y命令
1.#把1-3行內所有匹配line任意字母的字符轉變爲大寫。注意:正則表達式元字符不能使用這個命令。 [root@localhost mnt]# sed '1,3y/line/LINE/' example.txt oNE LINE tExt two LINE tExt thrEE LINE tExt four line text five line text end line !!!
退出:q命令
1.#打印第三行後退出sed。 [root@localhost mnt]# sed '3q' example.txt one line text two line text three line text
保持和獲取:h命令和G命令
1.#在sed處理文件的時候,每一行都被保存在一個叫模式空間的臨時緩衝區中, #除非行被刪除或者輸出被取消,否則所有被處理的行都將打印在屏幕上。 #接着模式空間被清空,並存入新的一行等待處理。 #這裏,匹配two的行存入模式空間,h命令將其複製並存入一個稱爲保持緩存區的特殊緩衝區內。 #第二條語句是當達到最後一行後,G命令取出保持緩衝區的行。 [root@localhost mnt]# sed -e '/two/h' -e '$G' example.txt one line text two line text three line text four line text five line text end line !!! two line text
保持和互換:h命令和x命令
1.#互換模式空間和保持緩衝區的內容。由結果可以得出匹配three的行將被two替換。 [root@localhost mnt]# sed -e '/two/h' -e '/three/x' example.txt one line text two line text two line text four line text five line text end line !!!
AWK學習:
使用方法:
awk '{pattern + action}' {filenames}
AWK內置變量:
ARGC #命令行參數個數 ARGV #命令行參數排列 ENVIRON #支持隊列中系統環境變量的使用 FILENAME #awk瀏覽的文件名 FNR #瀏覽文件的記錄數 FS #設置輸入域分隔符,等價於命令行 -F選項 NF #瀏覽記錄的域的個數 NR #已經讀出的記錄數,就是行號,從1開始,如果有多個文件話,這個值也是不斷累加中。 OFS #輸出字段分隔符, 默認也是空格 ORS #輸出的記錄分隔符,默認爲換行符 RS #輸入的記錄分隔符, 默認爲換行符 此外,$0變量是指整條記錄。$1表示當前行的第一個域,$2表示當前行的第二個域,......以此類推
AWK入門指南:http://awk.readthedocs.org/en/latest/chapter-one.html