- sed -e /^noshow/d test.txt
- sed -n /[^noshow]/ test.txt
- awd /[^noshow]/ test.txt
grep -v 'boshow' test.txt
grep 常用參數(shù)速記
- egrep 等同 grep -E
-C:打印出匹配的行的上下文前后各 n行。
-i: 不區(qū)分大小寫字符。
-n:在輸出的每行前面加上它所在的文件中它的行號,
-v: 也就是說反向匹配
-o : 顯示匹配的行中與 PATTERN 相匹配的部分。
-L:和上面的-l相對,顯示不匹配的文件名,
-R,-r: 遞歸地讀每一目錄下的所有文件。
^$:代表空行:
-e:該選型可以匹配多個模式
sed命令
sed全稱是stream editor ,他的特點是針對行來處理,一般配合正則表達式來處理。
參數(shù)速記
-n 使用安靜(silent)模式(想不通為什么不是-s)。
在一般sed的用法中,所有來自stdin的內(nèi)容一般都會被列出到屏幕上。
但如果加上-n參數(shù)后,則只有經(jīng)過sed特殊處理的那一行(或者動作)才會被列出來;
-r 讓sed命令支持擴展的正則表達式(默認是基礎(chǔ)正則表達式);
-i 直接修改讀取的文件內(nèi)容,而不是由屏幕輸出。
幾個重要的操作參數(shù)
a \: append即追加字符串, a \的后面跟上字符串s(多行字符串可以用\n分隔),
則會在當前選擇的行的后面都加上字符串s;
c \: 取代/替換字符串,c \后面跟上字符串s(多行字符串可以用\n分隔),則會將當前選中的行替換成字符串s;
d: delete即刪除,該命令會將當前選中的行刪除;
i \: insert即插入字符串,i \后面跟上字符串s(多行字符串可以用\n分隔),則會在當前選中的行的前面都插入字符串s;
p: print即打印,該命令會打印當前選擇的行到屏幕上;
s: 替換,通常s命令的用法是這樣的:1,2s/old/new/g,將old字符串替換成new字符串;其中的g 表示global全局替換,如果沒有g(shù)lobal的話,只會替換每一行中的第一個匹配的內(nèi)容;
=: 顯示文件行號
下面通過幾個例子快速復(fù)習下
sed '1a \add one' my.txt
sed '/sb/c \ 2d' my.txt
sed -n '1,4p' my.txt #顯示第1-4行;
sed 's/sb/2b/g' my.txt #替換;
sed '1,5d' my.txt #刪除;
正則表達式來替換
# 如果你這樣搞的話,就會有問題
$ sed 's/<.*>//g' html.txt
Understand?
# 要解決上面的那個問題,就得像下面這樣。
# 其中的'[^>]' 指定了除了>的字符重復(fù)0次或多次。
$ sed 's/<[^>]*>//g' html.txt
This is what I meant. Understand?
指定行替換,只替換第三行
$ sed "3s/my/your/g" pets.txt
This is my cat
my cat's name is betty
This is your dog
my dog's name is frank
This is my fish
my fish's name is george
This is my goat
my goat's name is adam
替換自定范圍,只替換第3到第6行的文本。
$ sed "3,6s/my/your/g" pets.txt
This is my cat
my cat's name is betty
This is your dog
your dog's name is frank
This is your fish
your fish's name is george
This is my goat
my goat's name is adam
只替換每一行的第一個s
$ sed 's/s/S/1' my.txt
ThiS is my cat, my cat's name is betty
ThiS is my dog, my dog's name is frank
ThiS is my fish, my fish's name is george
ThiS is my goat, my goat's name is adam
只替換每一行的第二個s
$ sed 's/s/S/2' my.txt
This iS my cat, my cat's name is betty
This iS my dog, my dog's name is frank
This iS my fish, my fish's name is george
This iS my goat, my goat's name is adam
只替換第一行的第3個以后的s
$ sed 's/s/S/3g' my.txt
This is my cat, my cat'S name iS betty
This is my dog, my dog'S name iS frank
This is my fiSh, my fiSh'S name iS george
This is my goat, my goat'S name iS adam
多個匹配
如果我們需要一次替換多個模式,可參看下面的示例:(第一個模式把第一行到第三行的my替換成your,第二個則把第3行以后的This替換成了That)
$ sed '1,3s/my/your/g; 3,$s/This/That/g' my.txt
This is your cat, your cat's name is betty
This is your dog, your dog's name is frank
That is your fish, your fish's name is george
That is my goat, my goat's name is adam
上面的命令等價于:(注:下面使用的是sed的-e命令行參數(shù))
sed -e '1,3s/my/your/g' -e '3,$s/This/That/g' my.txt
我們可以使用&來當做被匹配的變量,然后可以在基本左右加點東西。如下所示:
$ sed 's/my/[&]/g' my.txt
This is [my] cat, [my] cat's name is betty
This is [my] dog, [my] dog's name is frank
This is [my] fish, [my] fish's name is george
This is [my] goat, [my] goat's name is adam
圓括號匹配
使用圓括號匹配的示例:(圓括號括起來的正則表達式所匹配的字符串會可以當成變量來使用,sed中使用的是\1,\2…)
$ sed 's/This is my \([^,&]*\),.*is \(.*\)/\1:\2/g' my.txt
cat:betty
dog:frank
fish:george
goat:adam
上面這個例子中的正則表達式有點復(fù)雜,解開如下(去掉轉(zhuǎn)義字符):
正則為:This is my ([^,]),.is (.*)
匹配為:This is my (cat),……….is (betty)
然后:\1就是cat,\2就是betty
參考
http://10706198.blog.51cto.com/10696198/1792393
http://www.cnblogs.com/yinheyi/p/6653592.html