cut
# cut --help
用法:cut [选项]... [文件]...
Print selected parts of lines from each FILE to standard output.
如果没有指定文件,或者文件为"-",则从标准输入读取。
必选参数对长短选项同时适用。
-b, --bytes=列表 只选中指定的这些字节
-c, --characters=列表 只选中指定的这些字符
-d, --delimiter=分界符 使用指定分界符代替制表符作为区域分界
-f, --fields=列表 只选中指定的这些域;并打印所有不包含分界符的
行,除非-s 选项被指定
-n (忽略)
--complement 补全选中的字节、字符或域
-s, --only-delimited 不打印没有包含分界符的行
--output-delimiter=字符串 使用指定的字符串作为输出分界符,默认采用输入
的分界符
-z, --zero-terminated line delimiter is NUL, not newline
--help 显示此帮助信息并退出
--version 显示版本信息并退出
仅使用f -b, -c 或-f 中的一个。每一个列表都是专门为一个类别作出的,或者您可以用逗号隔
常用-f:截取第几列,-d:分隔符
例子:
以/etc/passwd下的文件为例:
root@jimo:~# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
......
hplip:x:136:7:HPLIP system user,,,:/var/run/hplip:/bin/false
vboxadd:x:999:1::/var/run/vboxadd:/bin/false
Debian-snmp:x:123:128::/var/lib/snmp:/bin/false
截取非root用户:grep -v "root"是取反
root@jimo:~# grep "/bin/bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
couchdb:x:117:124:CouchDB Administrator,,,:/var/lib/couchdb:/bin/bash
postgres:x:124:130:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
root@jimo:~# grep "/bin/bash" /etc/passwd | grep -v "root"
couchdb:x:117:124:CouchDB Administrator,,,:/var/lib/couchdb:/bin/bash
postgres:x:124:130:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
以:为分隔符的1和3列
root@jimo:~# grep "/bin/bash" /etc/passwd | grep -v "root" | cut -f 1,3 -d ":"
couchdb:117
postgres:124
cut不够智能,只能识别精确的间隔符:
root@jimo:~# df -h
文件系统 容量 已用 可用 已用% 挂载点
udev 3.8G 0 3.8G 0% /dev
tmpfs 770M 9.7M 760M 2% /run
/dev/sda1 211G 84G 117G 42% /
tmpfs 3.8G 60M 3.7G 2% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
tmpfs 770M 16K 770M 1% /run/user/133
tmpfs 770M 48K 770M 1% /run/user/0
root@jimo:~# df -h | cut -f 1,3 -d " "
文件系统
udev
tmpfs
/dev/sda1
tmpfs
tmpfs
tmpfs
tmpfs
tmpfs
printf
printf '输出类型' 内容
%ns:字符串,n为个数
%ni:整数
%m.nf:浮点数,m位,n个小数
# printf '%s\t%s\n' 1 2 3 4
1 2
3 4
输出文件内容:
root@jimo:~# cat test.txt
1 2 3 4
5 7 8 9
a d h k
root@jimo:~# printf '%s\n' $(cat test.txt)
1
2
3
4
5
7
8
9
a
d
h
k
root@jimo:~# printf '%s\t%s\t%s\t%s\n' $(cat test.txt)
1 2 3 4
5 7 8 9
a d h k
root@jimo:~#
awk
awk '条件1{动作1}条件2{动作2}...' 文件
条件:关系表达式
动作:格式化输出、流程控制语句
例子:一个没有条件的截取查找:第二列和第四列
root@jimo:~# cat test.txt
1 2 3 4
5 7 8 9
a d h k
root@jimo:~# awk '{printf $2 "\t" $4 "\n"}' test.txt
2 4
7 9
d k
取得磁盘占用比:
root@jimo:~# df -h | grep "/dev/sda1"
/dev/sda1 211G 84G 117G 42% /
root@jimo:~# df -h | grep "/dev/sda1"
/dev/sda1 211G 84G 117G 42% /
root@jimo:~# df -h | grep "/dev/sda1" | awk '{print $5}'
42%
root@jimo:~# df -h | grep "/dev/sda1" | awk '{print $5}' | cut -f 1 -d "%"
42
BEGIN:开始执行
END:结束执行
root@jimo:~# cat test.txt
1 2 3 4
5 7 8 9
a d h k
root@jimo:~# awk 'BEGIN{printf "BEGIN***\n"}$2>3{printf $2 "\t" $3 "\n"}END{print "END***"}' test.txt
BEGIN***
7 8
d h
END***
添加分隔符:
root@jimo:~# cat /etc/passwd | grep "/bin/bash" | awk 'BEGIN{FS=":"}{print $1 "/t" $3}'
root/t0
couchdb/t117
postgres/t124
sed
字符串替换
sed [选项] '动作' 文件名
动作:
a:行后追加、c:替换行、i:行前插入、p:打印、s:字符串替换、d:删除一行
选项:
-n:只输出sed操作的行
root@jimo:~# cat test.txt
1 2 3 4
5 7 8 9
a d h k
root@jimo:~# sed '2p' test.txt
1 2 3 4
5 7 8 9
5 7 8 9
a d h k
root@jimo:~# sed -n '2p' test.txt
5 7 8 9
-i:直接修改文件,不经过屏幕(慎用)
-e:应用多条sed动作
例子:
插入:
root@jimo:~# cat test.txt
1 2 3 4
5 7 8 9
a d h k
root@jimo:~# sed '1i insert 1' test.txt
insert 1
1 2 3 4
5 7 8 9
a d h k
删除:(是2-4行)
root@jimo:~# sed '2,4d' test.txt
1 2 3 4
追加:
root@jimo:~# sed '2a after 2 append' test.txt
1 2 3 4
5 7 8 9
after 2 append
a d h k
替换行:
root@jimo:~# sed '2c 替换第二行' test.txt
1 2 3 4
替换第二行
a d h k
字符串替换:
's/旧字符串/新字符串/g':g代表是否全局替换
root@jimo:~# sed '2s/8/88/g' test.txt
1 2 3 4
5 7 88 9
a d h k
-e的例子:中间用分号分隔:
# sed -e '2s/9/ee/g;1s/4/rr/g' test.txt
1 2 3 rr
5 7 8 ee
a d h k
sort
-f:忽略大小写
-n:当成数值排序
-r:取反
-t:指定分隔符
-k n[,m] 从第n个字符到第m个字符
以:为分隔符,第3个字符当成数值来排序
# sort -t ":" -k "3,3" -n /etc/passwd
...
beef-xss:x:134:140::/var/lib/beef-xss:/bin/false
dradis:x:135:141::/var/lib/dradis:/bin/false
hplip:x:136:7:HPLIP system user,,,:/var/run/hplip:/bin/false
clamav:x:137:143::/var/lib/clamav:/bin/false
mongodb:x:138:65534::/home/mongodb:/bin/false
vboxadd:x:999:1::/var/run/vboxadd:/bin/false
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
wc
统计字符数
-l:行数
-w:单词数
-m:字符数
-c:字节数
root@jimo:~# cat test.txt
1 2 3 4
5 7 8 9
a d h k
root@jimo:~# wc test.txt
3 12 42 test.txt
root@jimo:~# ls -lh test.txt
-rw-r--r-- 1 root root 42 2月 9 18:30 test.txt