awk
#查看第二行到第四行的數(shù)據(jù)
netstat -nltp | awk 'NR == 2, NR == 4'
#查看第二行和第四行的數(shù)據(jù)
netstat -nltp | awk 'NR == 2; NR == 4'
#輸出第4列和最后一列的數(shù)據(jù)
netstat -nltp | awk '{print $4恭金,$NF}'
#指定分隔符
awk -F ':' '{print $NF}'
#查詢第一列以tcp6 開頭的行
netstat -nltp | awk '$1~/^tcp6/{print}'
#組合
netstat -nltp | awk 'NR == 3, NR == 5 {print $4}' | awk -F ':' '{print $NF}'
sed
#在第一行插入一條記錄 (從第一個(gè)有效字符開始插入辽剧,空格忽略)
sed -i '1i hello,world' state
#在第一行刪除一條記錄
sed -i '1d hello,world' state
#在以 tcp6 開頭 行 添加 hello, 以 tcp6 結(jié)尾的行 添加 bye禁炒,tcp6
netstat -nltp | sed -e '/^tcp6/i hello, tcp6' -e '/tcp6$/a bye, tcp6'
# 修改以tcp6開頭的行廓块, 替換為 tcp8
sed "s/^tcp6/tcp8/g" state
-i 是寫入磁盤的意思
-e 是多個(gè)條件并行
grep
# 查找 0.0.0.0 的行, 忽略大小寫
netstat -nltp | grep -i "0.0.0.0"
# 查找 0.0.0.0 下3行
netstat -nltp | grep -a3 '0.0.0.0'
# 查找 0.0.0.0 上3行
netstat -nltp | grep -b3 '0.0.0.0'
# 查找 0.0.0.0 上、下3行
netstat -nltp | grep -c3 '0.0.0.0'