歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> AIX下的sed介紹

AIX下的sed介紹

日期:2017/2/28 14:55:38   编辑:Linux教程
sed:是一個非交互性文本流編輯器,可編輯大或小的文件,sed命令定時編輯,刪除文件;
一次性處理所有改變。
sed不與原文件打交道,只是操作源文件的一個拷貝,然後所有的改動輸出到一個文件,並輸出到屏幕。

調用sed的三種方式:
1.命令行鍵入命令
2.將sed命令插入腳本文件,然後調用sed
3.將sed命令插入腳本文件,並使sed腳本可執行?自動執行?

調用sed
1.命令格式
sed [選項] sed命令 輸入文件
2.使用sed腳本
sed [選項] -f sed腳本文件 輸入文件
3.使用第一行具有sed命令解釋器的sed腳本文件
sed腳本 [選項] 輸入文件

選項:
n 不打印
c 下一命令是編輯命令[無用]
f 正在調用sed腳本文件

保存sed命令,可以使用重定向方式:sed 'sed_cmd' input_file > outfile

sed浏覽文件時,默認從第一行開始浏覽,若想定位文本,有2種方式:
1.使用行號,單行或多行(范圍)
2.正則表達式

使用sed在文件中定位文本的方式
——————————————————————————————————————————————————————————————————
x x為行號,如1
x,y 表示行號范圍從x到y
/pattern/ 查詢包含模式的行。例如/disk/或/[a-z]/
/pattern/pattern/ 查詢包含兩個模式的行,例如/disk/disks/
pattern/,x 在給定行號上查詢包含模式的行,例如/ribbon/,3
x,/pattern/ 通過行號和模式查詢匹配行,3,/vdu/
x,y! 查詢不包含指定行號x和y的行,1,2!
——————————————————————————————————————————————————————————————————

sed編輯命令
————————————————————————————————————————————————————————
p 打印匹配行
= 顯示文件行號
a\ 在定位行號後附加新文本信息
i\ 在定位行號後插入新文本信息
d 刪除定位行
c\ 在新文本替換定位文本
s 使用替換模式替換相應模式
r 從另一個文本中讀取文本
w 寫文本到一個文件
q 第一個模式匹配完成後推出或立即推出
l 顯示與八進制ASCII代碼等價的控制字符
{} 在定位行執行的命令組
n 從另一個文本中讀取下一行,並附加在下一行
g 將模式2粘貼到/pattern n/
y 傳遞字符
n 延續到下一輸入行;允許跨行的模式匹配語句
——————————————————————————————————————————————————————————
建立一個文件:
vi quote.txt
pg quote.txt
the honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

打印首行:
sed -n '1p' quote.txt
The honeysuckle band played all night long for noly $90.

打印最後一行:
sed -n '$p' quote.txt
The local nurse Miss P.Neave was in attendance.

打印整個文件,格式:1,$p
sed -n '1,$p' quote.txt
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

打印指定行
sed -n '4p' quote.txt
The local nurse Miss P.Neave was in attendance.

使用正則表達式
打印包含was單詞的行;
sed -n '/was/'p quote.txt[or sed -n '/was/p' quote.txt]
It was an evening of splendid music and company.
The local nurse Miss P.Neave was in attendance.


檢索數據:打印出現以ing結尾單詞的行
sed -n '/.*ing/'p quote.txt
It was an evening of splendid music and company.

查找包含$符號的行:
sed -n '/\$/'p quote.txt
The honeysuckle band played all night long for noly $90.

篩選出一行,格式:line_num,/pattern/
sed -n '4,/was/'p quote.txt
The local nurse Miss P.Neave was in attendance.

打印每行的行號:
sed -e '=' quote.txt
1
The honeysuckle band played all night long for noly $90.
2
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:10.
4
The local nurse Miss P.Neave was in attendance.

打印包含was單詞的行的行號:
sed -n '/was/=' quote.txt
2
4

包含was的行:
sed -n '/was/'p quote.txt
It was an evening of splendid music and company.
The local nurse Miss P.Neave was in attendance.

打印包含music的行號,和行數據:
sed -e '/music/=' -n -e '/music/p' quote.txt
2
It was an evening of splendid music and company.
Copyright © Linux教程網 All Rights Reserved