1. 程式人生 > >linux shell大檔案操作

linux shell大檔案操作

查詢字串所在行 : grep -n “待查詢字串” “檔名”

顯示指定行資訊:sed -n '1,5p' “指定檔案”  表示顯示指定檔案第一至五行的資訊

--------------------------------------------------------------------------

sed關鍵Options介紹:

  • -n : 安靜模式。一般sed用法中,所有來自STDIN的資料都會被輸出到螢幕上,使用-n只有被sed處理的行才會列出來。如果不使用-n,使用sed列印時,會把輸入流和處理的資訊都列印一遍
  • a
    :append,追加文字
  • i:insert,插入文字
  • d:delete,刪除文字
  • s: 模式匹配替換
  • p:列印文字

sed使用示例

  1. 在指定行插入或追加: 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
  2. 刪除: d 
    a. 刪除test.txt第二行: sed ‘2d’ test.txt 
    b. 刪除test.txt符合正則表示式/fish
    的行: sed ‘/fish/d’ test.txt
  3. 修改文字:s 
    a. 將text.txt中love替換為like: sed “s/love/like/g” test.txt (/g表示全域性匹配)
  4. 列印文字: p 
    a. 輸出test.txt的第5-7行:sed -n ‘5,7p’ test.txt (-n的作用就顯示出來了,可以去除-n檢視效果)