1 watch?
watch命令可以用來監控一個命令的執行,周期性的執行監控的命令
Usage:
?watch [-dhntv] [--differences[=cumulative]] [--help] [--interval=] [--no-title] [--version]
options:
-d, --differences[=cumulative]highlight changes between updates(cumulative means highlighting is cumulative) ?#高亮顯示命令執行變化的部分
-h, --help print a summary of the options #打印幫助信息
?-n, --interval=seconds to wait between updates #指定周期性執行命令的時間(單位s,默認2s)
-v, --version print the version number #打印版本信息
-t, --no-title turns off showing the header #關閉頭部顯示信息
示例:
每隔2s查看系統網絡連接情況:
watch -d -n 2 netstat -ant
2 sed 文件編輯
語法
sed [-hnV][-e<script>][-f<script文件>][文本文件]
參數說明:
h 命令幫助提示
n 安靜工作模式,sed默認會輸出所有的內容,加上-n參數,會只顯示受影響的行數
V 顯示版本信息
e 執行后面的命令,表明后面的內容為命令,可用來執行多條命令
f 執行后面指定的辦好了sed命令的文件
命令說明:
a :新增,在指定行的后面添加新的一行,內容由a命令后面接的字符串決定
c :取代, 取代指定行(多行)的內容,替代的內容接在c命令的后面
d :刪除,刪除指定行
i :插入, 在指定行的前面添加一行新的內容,i命令后面可接新增的內容
p :打印,可指定行或用正則來匹配,常配合-n使用
s :取代,'1,2s/old/new/g'
示例:
在第一行的后面新增一行
sed -e '1a content' testfile
或
sed '1a\ content' testfile
(-e 可以省略,'\'可作為命令和命令參數的分隔符)
在指定行后面添加多行
sed '1a line1\
>line2' t.txt
添加多行要用'\'作為分隔
要在指定行的前面添加內容的話,a命令換成i命令即可,如
sed '1i content' testfile
查看目標文件的1,2行:
sed ?-n ?'1,2p' testfile
在目標文件中搜索含有'root'關鍵詞的行
sed -n ?'/root/p' testfile
刪除目標文件中的指定行
sed '1,2d' testfile
刪除目標文件中包含指定內容的行
sed '/root/d' testfile
替換指定行的內容
sed '1c content' testfile
替換目標文件中的內容
sed '1,3s/old/new/g' testfile
注意如果要想對目標文件所做的修改寫入目標文件的話,記得加上-i命令