查找字元串所在行 : grep -n “待查找字元串” “檔案名”
顯示指定行資訊:sed -n '1,5p' “指定檔案” 表示顯示指定檔案第一至五行的資訊
--------------------------------------------------------------------------
sed關鍵Options介紹:
-
: 安靜模式。一般sed用法中,所有來自STDIN的資料都會被輸出到螢幕上,使用-n隻有被sed處理的行才會列出來。如果不使用-n,使用sed列印時,會把輸入流和處理的資訊都列印一遍-n
-
:append,追加文本a
-
:insert,插入文本i
-
:delete,删除文本d
-
: 模式比對替換s
-
:列印文本p
sed使用示例
-
在指定行插入或追加: a, i
a. 在test.txt第一行前插入:sed “1 i This is a test file” test.txt
b. 在test.txt最後一行追加:sed “$ a This is the end of file” test.txt
-
删除: d
a. 删除test.txt第二行: sed ‘2d’ test.txt
b. 删除test.txt符合正規表達式
的行: sed ‘/fish/d’ test.txt/fish
-
修改文本:s
a. 将text.txt中love替換為like: sed “s/love/like/g” test.txt (/g表示全局比對)
-
列印文本: p
a. 輸出test.txt的第5-7行:sed -n ‘5,7p’ test.txt (-n的作用就顯示出來了,可以去除-n檢視效果)
版權聲明:本文為CSDN部落客「weixin_33671935」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:https://blog.csdn.net/weixin_33671935/article/details/92404792