Shell Commands
[TOC]
合并行
$ cat test.txt
1 2
3 4
5 6
7 8
奇偶行合并
$ sed "N;s/\n/ /" test.txt
1 2 3 4
5 6 7 8
指定行合并
第三,四行合并
$ sed "3N;s/\n/ /" test.txt
1 2
3 4
5 6 7 8
所有行合并
$ sed ':a;N;$!ba;s/\n/ /g' test.txt
1 2 3 4 5 6 7 8
:a
:label
$!
:尾行不执行
ba
:无条件跳转到 label a
删除行
$ cat test.txt
1 2 3
3 4 5
5 6 7
7 8 9
删除含有 3 的行
$ sed "/3/d" test.txt
5 6 7
7 8 9
删除含有 3,4 的行
#sed "1,${/3/{/4/d}}" test.txt
$ sed "{/3/{/4/d}}" test.txt
1 2 3
5 6 7
7 8 9
插入行
插入第一行
$ sed "1i 10 11 12" test.txt
10 11 12
1 2 3
3 4 5
5 6 7
7 8 9
追加尾行
$ sed "$ a 10 11 12" test.txt
1 2 3
3 4 5
5 6 7
7 8 9
10 11 12
跳过行
$ sed "s/ /,/g;n" test.txt
1,2,3
3 4 5
5,6,7
7 8 9
跳过第三行(2+1)
$ sed "s/ /,/g;2n" test.txt
1,2,3
3,4,5
5 6 7
7,8,9
重复行
只保留第一次出现的行
$ cat test.txt
1 2 1
1 2 2
1 2 3
1 2 4
1 2 5
2 3 1
2 3 2
2 3 3
2 3 4
2 3 5
按第 1 列
$ awk '!a[$1]++ {print}' test.txt
1 2 1
2 3 1
按第 2 列
$ awk '!a[$2]++ {print}' test.txt
1 2 1
2 3 1
显示行数
$ awk 'END {print NR}' test.txt
10
$ wc -l test.txt
10
删除 RC 状态的包(Debian)
r: the package is marked for removal.
c: the configuration files are currently present in the system.
dpkg -l | grep ^rc | cut -d' ' -f3| sudo xargs dpkg -P
显示根的磁盘信息
df -h | awk '/\/$/ {print "disk total:" $2 " free:" $4}'
CPU 最近 5 次 id 信息
vmstat 1 5 | awk 'NR>=3 {id += $15} END {print "cpu id:" id/5}'
显示特定 app 内存
ps -e -o 'pid,args,user,pmem' | grep "${APP_NAME}" | grep -v 'grep' | awk '{print "app use mem:" 0.15*$4 "G"}'
显示 TCP 各个状态连接数
$ ss -ant | awk '{a[$1]++} END{for(i in a) {print i, a[i]}}'
ESTAB 30
LISTEN 6
$ ss -ant | grep "ESTAB" | wc -l
30
Last updated
Was this helpful?