sed,awk,grep使用小結

$ more t1.sh

#!/bin/sh

#fffff

helloworld ()

{

echo "test ffffff"

}

hellowworld

echo1 xxx ccc

 

1.       查找以echo1開頭的行 並獲得行號

$ sed -n '/^echo1/=' t1.sh

8

$ grep -n '^echo1' t1.sh | cut -d ':' -f 1

8

$ awk '{if($0 ~ /^echo1/) print FNR}' t1.sh

8

2.       獲得文件的行數

$ awk 'END{print NR}' t1.sh

8

$ sed -n '$=' t1.sh

8

$ grep -c "" t1.sh

8

3.       查找包含test並且後面爲空格的行並輸出

$ grep  'test /+' t1.sh

echo "test ffffff"

$ sed -n '/test /+/p' t1.sh

echo "test ffffff"

$ awk '{if($0 ~ /.*test +/) print $0}' t1.sh

echo "test ffffff"

 

sed命令參數連用 輸入結果和行號

$ sed -n -e '/test /+/p' -e '/test /+/=' t1.sh

echo "test ffffff"

5

 

4.        Sed刪除指定行

  刪除第一行

$ sed '1d' t1.sh

#fffff

helloworld ()

{

echo "test ffffff"

}

hellowworld

echo1 xxx ccc

 

如果要改變t1.sh的內容不能直接將結果定向到t1.sh,否則會將t1.sh至空。

這裏1d 1p ,1,2p,$p用法是一樣的

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章