79.文本操作命令:sed awk
這些命令操作文本時,是一行一行地處理的,如果該行匹配到了關鍵字或者正則表達式,那麼該行被接受並打印在屏幕上,然後讀取下一行並進行操作。
sed操作符不會修改原本的文件,如果想得到過濾後的結果,需要重定向到其他文件中。
sed過濾打印的語法格式:
sed -n '/Regular_Expression/p' filename
sed -n '/Mango/p' filename
sed替換字符串的語法格式:
即使替換後,原始文本文件還是不會動的。
sed -n 's/RE/replacement string/' filename
sed -n 's/Mango/Apple/' filename
Metacharacter | Function |
---|---|
^ | 指定一行的開頭字符 |
$ | 指定一行的結尾字符 |
. | 匹配一個字符,但不是某行的開頭字符 |
* | 匹配0個或多個字符 |
[] | 匹配中括號集合內的一個字符 |
[^] | 匹配非中括號集合內的任一個字符 |
(…) | 保存匹配到的字符 |
& | 保存匹配到的字符串,這樣就可以在替代字符串中拿到,下面有個例子就用到了& |
< | 一個單詞的開頭 |
> | 一個單詞的結尾 |
x{m} | 字符x的重複個數 |
x{m,} | 字符x至少重複個數 |
x{m,n} | 字符x重複m到n個之間 |
80.addressing in sed
sed 'command' filename
這樣會刪除掉1-3行
cat myfile | sed '1,3d'
sed '1,3d' myfile
打印item.list文件中的包含apple或者Apple的行,注意是打印整行
sed -n '/[Aa]pple/p' item.list
刪除匹配到的行
sed '/Apple/d' item.list
刪除未匹配到的行
sed '/Apple/!d' item.list
因爲sed命令並不會修改原文件,所以只能將輸出重定向到臨時文件.
sed '1,3d' datafile > tempfile
mv tempfile newfile
-n:是否顯示默認輸出
sed的-p命令在輸出時,是默認將文本文件中的讀取的每一行都輸出到終端的,包括匹配到的和未匹配到的,那麼如何控制sed命令只輸出匹配到的呢?so,-n 命令能做到。
格式:
sed -n '/USA/p' country.txt #將只輸出包含USA的行
-d:刪除匹配到的行。
sed '3d' country.txt #刪除掉第3行
sed '3,$d' country.txt#刪除掉3-last行 $:最後一行
sed '$d' country.txt#刪除最後一行
sed '/Japan/d' country.txt #刪掉匹配到japan的行
sed '/Japan/!d' country.txt #刪掉沒匹配到japan的行
sed '4,+5d' #刪除第4行,以及接下來的5行
sed '1,5!d' #留住1-5行,刪除所有的其他行
sed '1~3d' # 刪除1,4,7... 3的等差數列
sed '2~2d' #刪除偶數行
-s:替換命令
語法格式:
sed 's/Caashew/Almonds/g' shopping.txt #將文件中的Cashew替換爲Almonds,如果不加g,只替換第一個
例子:
sed 's/[0-9][0-9]$/&.5/' #匹配規則:以2個數字結尾;替換規則:在後面加上.5 注意&字符代表前面匹配到的兩個數字。
,:範圍限制
‘6d’:第6行
‘3,6d’:第3-6行
‘/pattern1/,/pattern2/’:格式1和格式2之間的所有行
‘/pattern1/,4’:格式1和第4行之間
例子:
cat country.txt
output:
Country Capital ISD Code
USA Washington 1
China Beijing 86
Japan Tokyo 81
India Delhi 91
sed -n '/USA/,/Japan/p' country.txt
output:
USA Washington 1
China Beijing 86
Japan Tokyo 81
sed -n '2,/India/p' country.txt
output:
USA Washington 1
China Beijing 86
Japan Tokyo 81
India Delhi 91
cat shopping.txt
output:
Product Quantity Unit_Price Total_Cost
Apple 2 3 6
Orange 2 .8 1.6
Papaya 2 1.5 3
Chicken 3 5 15.5
Cashew 1 10 10.5
#這條命令分2部分:
第一部分:'/Apple/,/Papaya/' 表示匹配兩個pattern之間的行
第二部分:'s/$/*** out of stock ***/' 表示替換經過第一個命令過濾後的行
sed '/Apple/,/Papaya/s/$/** Out of stock ***/' shopping.txt
output:
Product Quantity Unit_Price Total_Cost
Apple 2 3 6** Out of stock ***
Orange 2 .8 1.6** Out of stock ***
Papaya 2 1.5 3** Out of stock ***
Chicken 3 5 15.5
Cashew 1 10 10.5
-e:連接多個命令
連續執行多個命令
語法:
sed -e command1 -e command2 -e conmmand3 fileName
sed -e '5d' -e 's/Cashew/Almonds/' shopping.txt
-r:將一個文件中的內容插入到另一個文件中(只是在終端打印出結果,並不會真正的改變目標文件)。
語法:
sed ‘/Apple/r new.txt’ shopping.txt # 表示將new.txt中的內容插入到shopping.txt中匹配到Apple的行後面
-w:將一個文件中的內容寫入到另一個文件中,會執行寫入操作,真正得改變文件。
語法
sed -n ‘/Chicken/w new.txt’ shopping.txt
將shopping.txt中匹配到Chicken的行的文本寫到new.txt中
a:append命令,添加一行到文件中(並不會真的修改文件),插入到匹配的行的後面。
語法:
在shopping.txt的匹配到oragne的行後面插入這句話。
sed '/Orange/a\
***** Buy one get one free offer on this item!*****' shopping.txt
i:與a命令一樣,插入行到文件中,不過插入到匹配的行的前面。
語法:
sed '/Apple/i\
New prices will apply from Next month! ' shopping.txt
c:替換文本內容,使用指定的內容來替換匹配到的整行。
語法:
sed '/Papaya/c\
Papaya is out of stock today !
' shopping.txt
y:字符轉換操作符。
語法:y前面是範圍限定符
sed '2,4y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRS
TUVWXYZ/' shopping.txt
sed '/Apple/,/Chicken/y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' shopping.txt
q:退出sed處理程序,不再執行接下來的行
sed '3q' shopping.txt #只打印前3行
h 和 g: 存儲與取回命令。g會將存儲的內容append到文本的最後面。
sed -e '/Product/h' -e '$g' shopping.txt #與s命令中的&的區別是,&可以獲取匹配到的字符串,而h/g是獲取匹配到的整行。
h和x:存儲與替換命令。x會用存儲的內容替換掉匹配到的內容。
sed -e '/Apple/h' -e '/Cashew/x' shopping.txt # Cashew行將會被Apple行替換掉。
81.sed腳本。
語法格式:
sed -f sed_script_name target.txt
sed腳本中:
每個命令要換行的話,用\換行;
多個命令,想不換行的話,用:隔開。
# 第一個sed腳本
# 在第一行之前插入
1i\
Stock status report
/Orange/a\
Fresh Oranges are not available in this season. \
Fresh Oranges will be available from next month
/Chicken/c\
**********************************************************\
We will not be stocking this item for next few weeks.\
**********************************************************
$d
#$d表示刪除最後一行。