[root@iz2ze76ybn73dvwmdij06zz ~]# cat monkey One day,a little monkey is playing by the well.一天,有只小猴子在井邊玩兒. He looks in the well and shouts :它往井里一瞧,高喊道: “Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里頭啦!” An older monkeys runs over,takes a look,and says,一只大猴子跑來一看,說, “Goodness me!The moon is really in the water!” “糟啦!月亮掉在井里頭啦!” And olderly monkey comes over.老猴子也跑過來. He is very surprised as well and cries out:他也非常驚奇,喊道: “The moon is in the well.” “糟了,月亮掉在井里頭了!” A group of monkeys run over to the well .一群猴子跑到井邊來, They look at the moon in the well and shout:他們看到井里的月亮,喊道: “The moon did fall into the well!Come on!Let’get it out!” “月亮掉在井里頭啦!快來!讓我們把它撈起來!” Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch . 然后,老猴子倒掛在大樹上, And he pulls the next monkey’s feet with his hands.拉住大猴子的腳, All the other monkeys follow his suit,其他的猴子一個(gè)個(gè)跟著, And they join each other one by one down to the moon in the well. 它們一只連著一只直到井里. Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢 He yells excitedly “Don’t be so foolish!The moon is still in the sky!” 它興奮地大叫:“別蠢了!月亮還好好地掛在天上呢!
直接查找符合條件的行
[root@iz2ze76ybn73dvwmdij06zz ~]# grep moon monkey “Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里頭啦!” “Goodness me!The moon is really in the water!” “糟啦!月亮掉在井里頭啦!” “The moon is in the well.” “糟了,月亮掉在井里頭了!” They look at the moon in the well and shout:他們看到井里的月亮,喊道: “The moon did fall into the well!Come on!Let’get it out!” And they join each other one by one down to the moon in the well. Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢 He yells excitedly “Don’t be so foolish!The moon is still in the sky!”
查找反向符合條件的行
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -v moon monkey One day,a little monkey is playing by the well.一天,有只小猴子在井邊玩兒. He looks in the well and shouts :它往井里一瞧,高喊道: An older monkeys runs over,takes a look,and says,一只大猴子跑來一看,說, And olderly monkey comes over.老猴子也跑過來. He is very surprised as well and cries out:他也非常驚奇,喊道: A group of monkeys run over to the well .一群猴子跑到井邊來, “月亮掉在井里頭啦!快來!讓我們把它撈起來!” Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch . 然后,老猴子倒掛在大樹上, And he pulls the next monkey’s feet with his hands.拉住大猴子的腳, All the other monkeys follow his suit,其他的猴子一個(gè)個(gè)跟著, 它們一只連著一只直到井里. 它興奮地大叫:“別蠢了!月亮還好好地掛在天上呢!”
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -i my monkey “Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里頭啦!”
查找符合條件的行并輸出行號
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -n monkey monkey 1:One day,a little monkey is playing by the well.一天,有只小猴子在井邊玩兒. 4:An older monkeys runs over,takes a look,and says,一只大猴子跑來一看,說, 6:And olderly monkey comes over.老猴子也跑過來. 9:A group of monkeys run over to the well .一群猴子跑到井邊來, 13:Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch . 15:And he pulls the next monkey’s feet with his hands.拉住大猴子的腳, 16:All the other monkeys follow his suit,其他的猴子一個(gè)個(gè)跟著, 19:Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢
查找開頭是J的行
[root@iz2ze76ybn73dvwmdij06zz ~]# grep '^J' monkey Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢
查找結(jié)尾是呢的行
[root@iz2ze76ybn73dvwmdij06zz ~]# grep "呢$" monkey Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他們摸到月亮的時(shí)候,老猴子抬頭發(fā)現(xiàn)月亮掛在天上呢
d 刪除選擇的行 s 查找 y 替換 i 當(dāng)前行前面插入一行 a 當(dāng)前行后面插入一行 p 打印行 q 退出
替換符:
數(shù)字 :替換第幾處 g : 全局替換 \1: 子串匹配標(biāo)記,前面搜索可以用元字符集\(..\) &: 保留搜索刀的字符用來替換其他字符
操作:
替換
查看文件:
? happy cat word Twinkle, twinkle, little star How I wonder what you are Up above the world so high Like a diamond in the sky When the blazing sun is gone
替換:
? happy sed 's/little/big/' word Twinkle, twinkle, big star How I wonder what you are Up above the world so high Like a diamond in the sky When the blazing sun is gone
查看文本:
? happy cat word1 Oh if there's one thing to be taught it's dreams are made to be caught and friends can never be bought Doesn't matter how long it's been I know you'll always jump in 'Cause we don't know how to quit
全局替換:
? happy sed 's/to/can/g' word1 Oh if there's one thing can be taught it's dreams are made can be caught and friends can never be bought Doesn't matter how long it's been I know you'll always jump in 'Cause we don't know how can quit
按行替換(替換2到最后一行)
? happy sed '2,$s/to/can/' word1 Oh if there's one thing to be taught it's dreams are made can be caught and friends can never be bought Doesn't matter how long it's been I know you'll always jump in 'Cause we don't know how can quit
刪除:
查看文本:
? happy cat word Twinkle, twinkle, little star How I wonder what you are Up above the world so high Like a diamond in the sky When the blazing sun is gone
刪除:
? happy sed '2d' word Twinkle, twinkle, little star Up above the world so high Like a diamond in the sky When the blazing sun is gone
顯示行號:
? happy sed '=;2d' word 1 Twinkle, twinkle, little star 2 3 Up above the world so high 4 Like a diamond in the sky 5 When the blazing sun is gone
刪除第2行到第四行:
? happy sed '=;2,4d' word 1 Twinkle, twinkle, little star 2 3 4 5 When the blazing sun is gone
添加行:
向前插入:
? happy echo"hello" | sed 'i\kitty' kitty hello
向后插入:
? happy echo"kitty" | sed 'i\hello' hello kitty
修改行:
替換第二行為hello kitty
? happy sed '2c\hello kitty' word Twinkle, twinkle, little star hello kitty Up above the world so high Like a diamond in the sky When the blazing sun is gone
替換第二行到最后一行為hello kitty
? happy sed '2,$c\hello kitty' word Twinkle, twinkle, little star hello kitty
寫入行
把帶star的行寫入c文件中,c提前創(chuàng)建
? happy sed -n '/star/w c' word ? happy cat c Twinkle, twinkle, little star
退出
打印3行后,退出sed
? happy sed '3q' word Twinkle, twinkle, little star How I wonder what you are Up above the world so high
awk
名字由來
創(chuàng)始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首個(gè)字母。
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print "Name Math Chinese English History Sport grade\n----------------------------------------------"} {print $0}' students_store
Name Math Chinese English History Sport grade ---------------------------------------------------------- Xiaoka 60 80 40 90 77 class-1 Yizhihua 70 66 50 80 90 class-1 kerwin 80 90 60 70 60 class-2 Fengzheng 90 78 62 40 62 class-2
僅打印姓名、數(shù)學(xué)成績、班級信息,再加一個(gè)文尾(再接再厲):
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print "Name Math grade\n---------------------"} {print $1 2 "\t" $7} END {print "continue to exert oneself"}' students_store
Name Math grade --------------------- Xiaoka 60 class-1 Yizhihua 70 class-1 kerwin 80 class-2 Fengzheng 90 class-2 continue to exert oneself