sed用法:
sed 'command' filename(s) 只顯示結果而不修改檔案。
1、sed '2,5d' file 顯示檔案file,除去2-5行,但行數超過檔案實際行數時不會報錯。
sed '/10[1-4]/d' file 顯示檔案file,除去包含101-104的行。
sed '2,$d' file 顯示檔案,只顯示第一行。sed '2,$!d' file則只顯示除第一行外的其它行。
sed '/^ *$/d file 刪除檔案中的空行。
2、sed -n '/10[1-4]/p' file 只顯示檔案file中包含101-104的行。(-n和p必須同時使用,否則只有p時顯示全部檔案並多顯
示一次找到的行)
sed -n '5p' file 只顯示檔案的第5行
3、sed 's/moding/moden/g' file 將moding替換為moden
4、sed -n 's/^west/north/p' file 將west開頭的行替換為north並顯示出來。
5、sed 's/[0-9][0-9][0-9]$/&.5/' file將file檔案中以3個數字結尾的行替換為原數字加".5",&代表搜尋到的字串。
6、sed 's/\(mod\)ing/\1en/g file 將mod做為模式1封裝在括號裡,然後替換。
sed 's/...$//' file 刪除每一行的最後三個字元。
sed 's/^...//' file 刪除每一行的頭三個字元。
7、sed 's#moding#moden#g' file 將moding替換為moden,s後面的#代表搜尋串和替換串之間的分界符。
8、sed -n '/101/,/105/p' file 顯示從101的匹配行到105的匹配行。如果只找到101的匹配行,則從101的匹配行到檔案末。
sed -n '2,/999/p' file 顯示從第2行到匹配行。
9、sed '/101/,/105/s/$/ 20050119/' file將從101的匹配行到105的匹配行的行末增加" 20050119"內容。
10、sed -e '1,3d' -e 's/moding/moden/g' file 先刪除檔案的1-3行,再進行替換。
sed -e '/^#/!d' file 顯示檔案以#開頭的行。
11、sed '/101/r newfile' file 在每個匹配行增加檔案newfile的內容
sed '/101/w newfile' file 把匹配行寫入newfile。
12、sed '/101/a\
> ###' file 在匹配行後增加一新行。
sed '/101/i\
> ###' file 在匹配行前增加一新行。
sed '/101/c\
> ###' file 用新行替換匹配行。
13、sed 'y/abcd/abcd/' file 將a、b、c、d分別替換為abcd。
14、sed '5q' file 顯示到第5行時退出。
15、sed '/101/' file 在檔案中找到匹配行的後一行(n)再進行替換。
sed '/101/' file 在檔案中找到第乙個匹配行後進行替換後再退出。
16、sed -e '/101/' -e '/104/' file 在檔案中找到與101匹配行後先存在乙個快取中,再放在與104匹配行後。
sed -e '/101/' -e '/104/' file 在檔案中找到與101匹配行後先存在乙個快取中,再替代104的匹配行。
sed -e '/101/h' -e '$g' file 將最後乙個匹配行放在檔案末。
sed -e '/101/h' -e '$g' file 將最後乙個匹配行替換檔案末行。
sed -e '/101/h' -e '/104/x' file 在檔案中找到與101匹配行後先存在乙個快取中,再與104的匹配行進行互換
。17、sed -f sfile file 根據檔案sfile的命令列表進行操作。
cat sfile
/101/a\
####101####\
****101****
/104/c\
####104 deleted####\
****104 deleted****
1i\####test####\
****test****
sed 使用小結
sed可用來編輯文字,主要作用是查詢和替換 1.查詢 sed n ahcming p以上作用等同於 grep ahcming 使用 n,sed將不會在操作完成後列印,只有p才表示輸出 2替換sed 命令列 sed s 查詢內容 替換內容 sed s 查詢內容 替換內容 g 行為模式 每次讀取一行,查...
sed 命令小結
sed i s a b g filename i 直接在檔案中替換 不加 i輸出在終端,檔案不替換 g替換所有匹配字元 不加則替換第乙個 sed n s a b gp filename 將所替換的那一行列印到終端,不加 n則列印整個檔案 readonly aa 將乙個變數設定為唯讀 unset aa...
sed基礎用法
用法 sed option 位址命令 檔案 常用選項 n 不列印模式空間的內容到螢幕上 預設是列印的 e 多點編輯 sed optin e e f path script file 從指定檔案中讀取編輯指令碼 r 支援使用擴充套件正規表示式 i.bak 備份檔案並原處編輯 位址範圍 不給位址 對全文...