cut 选项 文件名
-f 列号 提取第几列
-d 分隔符 指定分隔符把行分成多列
不能以空格为分隔符。
[root@localhost ~]# cat testfile
no. name sex score
zhangsan m
lisi f
wangwu m
[root@localhost ~]# cut -f testfile 注释:默认分隔符是制表符\t
name
zhangsan
lisi
wangwu
[root@localhost ~]# cut -f , testfile
name score
zhangsan
lisi
wangwu
[root@localhost ~]# cat testfile
no. na:me sex sco:re
zhang:san m :
li:si f :
wang:wu m :
[root@localhost ~]# cut -f , -d : testfile
no. na:re
zhang:
li:
wang:
root@localhost ~]# cut -f -d : /etc/passwd|grep xiong
xiongjiawei
[root@localhost ~]# grep xiong /etc/passwd|cut -d : -f
xiongjiawei
[root@localhost ~]# grep xiong /etc/passwd|cut -d ":" -f
xiongjiawei
[root@localhost ~]# grep xiong /etc/passwd|cut -f -d :
xiongjiawei
[root@localhost ~]# grep xiong /etc/passwd|cut -f -d ":"
xiongjiawei
printf '格式' 输出内容
%ns 输出n个字符串
%ni 输出n个数字
%n.mf 输出共n位数字,m位小数,如%5.2f表示共3位整数,2位小数
\a 警告声
\b Backspace键
\f 清屏
\n 换行,常用
\r Enter,常用
\t Tab,常用
\v 垂直Tab
awk支持printf和print(linux默认无此命令),print输出会自动加换行,printf是标准格式输出命令,不会自动加换行,如果要换行需要手动加。
[root@localhost ~]# printf %s a b cd ef g
abcdefg12[root@localhost ~]# printf %s %s a b cd ef g
%sabcdefg12[root@localhost ~]# printf '%s %s' a b cd ef g
a bcd efg [root@localhost ~]# printf '%s\n%s' a b cd ef g
a
bcd
efg [root@localhost ~]# printf '%s %s\n' a b cd ef g
a b
cd ef
g [root@localhost ~]# printf %s $(cat testfile)
no.na:mesexsco:re1zhang:sanm8:82li:sif8:93wang:wum8:[root@localhost ~]#
[root@localhost ~]# cat testfile
no. na:me sex sco:re
zhang:san m :
li:si f :
wang:wu m :
awk '条件1{动作1} 条件2{运行2}...' 文件名
默认以空格为分隔符,执行命令时首先读取文件一行。
[root@localhost ~]# cat testfile
no. name sex score age comment
zhansan m student
li:si f member
wangwu m teacher
[root@localhost ~]# awk {printf $ $} testfile
awk: cmd. line:: {printf
awk: cmd. line:: ^ unexpected newline or end of string 注释:报错的原因是awk命令后的条件动作未加单引号
[root@localhost ~]# awk '{printf $2 $4}' testfile
namescorezhansan88li:si89wangwu87[root@localhost ~]# awk '{printf $2\t$4\n}' testfile
awk: cmd. line:: {printf $\t$\n}
awk: cmd. line:: ^ backslash not last character on line
awk: cmd. line:: {printf $\t$\n}
awk: cmd. line:: ^ syntax error 注释:报错是因为制表符\t未加双引号
[root@localhost ~]# awk '{printf $2 "\t" $4 \n}' testfile
awk: cmd. line:: {printf $ "\t" $ \n}
awk: cmd. line:: ^ backslash not last character on line
awk: cmd. line:: {printf $ "\t" $ \n}
awk: cmd. line:: ^ syntax error 注释:报错是因为换行符\n未加双引号
[root@localhost ~]# awk '{printf $2 "\t" $4 "\n"}' testfile
name score
zhansan
li:si
wangwu
[root@localhost ~]# awk '{printf $2"\t"$4"\n"}' testfile
name score
zhansan
li:si
wangwu
[root@localhost ~]# df -h|awk '{printf $1}'
文件系统/dev/sda5devtmpfstmpfstmpfstmpfs/dev/sda2/dev/sdb5/dev/sdb1/dev/sda1tmpfs[root@localhost ~]# df -h|awk '{printf $1"\n"}'
文件系统
/dev/sda5
devtmpfs
tmpfs
tmpfs
tmpfs
/dev/sda2
/dev/sdb5
/dev/sdb1
/dev/sda1
tmpfs
[root@localhost ~]# df -h|awk '{print $1}'
文件系统
/dev/sda5
devtmpfs
tmpfs
tmpfs
tmpfs
/dev/sda2
/dev/sdb5
/dev/sdb1
/dev/sda1
tmpfs
[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda5 % /
devtmpfs % /dev
tmpfs % /dev/shm
tmpfs % /run
tmpfs % /sys/fs/cgroup
/dev/sda2 % /home
/dev/sdb5 % /disk5
/dev/sdb1 % /disk1
/dev/sda1 % /boot
tmpfs % /run/user/
[root@localhost ~]# df -h|grep sda5
/dev/sda5 16G .4G 15G % /
[root@localhost ~]# df -h|grep sda5|awk '{print $5}'
%
[root@localhost ~]# df -h|grep sda5|awk '{print $5}'|cut -d % -f1 注释:cut命令的-f选项后的列序号和f之间可以无空格 [root@localhost ~]# df -h|grep sda5|awk '{print $5}'|cut -d % -f [root@localhost ~]# df -h|grep sda5|awk 'BEGIN{print "This is a title:"}{print $5}'|cut -d % -f
This is a title: [root@localhost ~]# df -h|grep sda5|awk '{print "This is a title:"}{print $5}'|cut -d % -f1
This is a title: [root@localhost ~]# awk '{print $1}' /etc/passwd 注释:awk的默认分隔符是空格
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
daemon:x:::daemon:/sbin:/sbin/nologin
adm:x:::adm:/var/adm:/sbin/nologin
lp:x:::lp:/var/spool/lpd:/sbin/nologin
sync:x:::sync:/sbin:/bin/sync
shutdown:x:::shutdown:/sbin:/sbin/shutdown
halt:x:::halt:/sbin:/sbin/halt
mail:x:::mail:/var/spool/mail:/sbin/nologin
operator:x:::operator:/root:/sbin/nologin
games:x:::games:/usr/games:/sbin/nologin
ftp:x:::FTP
nobody:x:::Nobody:/:/sbin/nologin
systemd-bus-proxy:x:::systemd
systemd-network:x:::systemd
dbus:x:::System
polkitd:x:::User
libstoragemgmt:x:::daemon
abrt:x::::/etc/abrt:/sbin/nologin
rpc:x:::Rpcbind
tss:x:::Account
postfix:x::::/var/spool/postfix:/sbin/nologin
sshd:x:::Privilege-separated
ntp:x::::/etc/ntp:/sbin/nologin
chrony:x::::/var/lib/chrony:/sbin/nologin
tcpdump:x::::/:/sbin/nologin
xiongjiawei:x:::xiongjiawei:/home/xiongjiawei:/bin/bash
qiaofeng:x::::/home/qiaofeng:/bin/bash
yangguo:x::::/home/yangguo:/bin/bash
st:x::::/home/st:/bin/bash
user1:x::::/home/user1:/bin/bash
[root@localhost ~]# awk '{FS=":"}{print $1}' /etc/passwd
root:x:::root:/root:/bin/bash 注释:设置分隔符为冒号:后第一行并未处理,因为awk命令的执行是首先读取第一行
bin
daemon
adm
lp
sync
shutdown
halt
operator
games
ftp
nobody
systemd-bus-proxy
systemd-network
dbus
polkitd
libstoragemgmt
abrt
rpc
tss
postfix
sshd
ntp
chrony
tcpdump
xiongjiawei
qiaofeng
yangguo
st
user1
[root@localhost ~]# awk 'BEGIN{FS=":"}{print $1}' /etc/passwd 注释:加BEGIN后就不会首先读取第一行,而是首先设置分隔符
root
bin
daemon
adm
lp
sync
shutdown
halt
operator
games
ftp
nobody
systemd-bus-proxy
systemd-network
dbus
polkitd
libstoragemgmt
abrt
rpc
tss
postfix
sshd
ntp
chrony
tcpdump
xiongjiawei
qiaofeng
yangguo
st
user1
[root@localhost ~]# awk 'END{print "The end!"}BEGIN{FS=":"}{print $1}' /etc/passwd 注释:有BEGIN就有END,在命令动作中的顺序无所谓
root
bin
daemon
adm
lp
sync
shutdown
halt
operator
games
ftp
nobody
systemd-bus-proxy
systemd-network
dbus
polkitd
libstoragemgmt
abrt
rpc
tss
postfix
sshd
ntp
chrony
tcpdump
xiongjiawei
qiaofeng
yangguo
st
user1
The end!
[root@localhost ~]# cat testfile
no. name sex score age comment
zhansan m student
li:si f member
wangwu m teacher
[root@localhost ~]# awk '$4>88{print $2}' testfile 注释:根据条件执行动作
name
li:si
sed
sed [选项] '[动作]' 文件名
选项:
-n 只输出sed处理过的行
-i 修改原文件
-e 执行多条sed动作
动作:
p 打印,例2p打印第2行,2,3p打印第2、3行
d 删除,例‘3,4d',不输出第3、4行
a 追加,例'2a Welcome to China',在第2行后换行追加Welcom to China
i 插入,例'2i Welcom to China',在第2行前插入Welcom to China,即在第1行后换行插入Welcom to China
c 行替换
s 字符串替换
[root@localhost ~]# cat testfile
no. name sex score age comment
zhansan m student
li:si f member
wangwu m teacher
[root@localhost ~]# sed '2p' testfile 注释:不加选项-n时即输出原文件又输出经过处理的结果
no. name sex score age comment
zhansan m student
zhansan m student
li:si f member
wangwu m teacher
[root@localhost ~]# sed -n '2p' testfile
zhansan m student
[root@localhost ~]# sed -n '2,3p' testfile
zhansan m student
li:si f member
[root@localhost ~]# df -h|sed -n '2p'
/dev/sda5 16G .4G 15G % /
[root@localhost ~]# sed '3,4d' testfile
no. name sex score age comment
zhansan m student
[root@localhost ~]# df -h|sed '2a This is added words'
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda5 16G .4G 15G % /
This is added words
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda2 .0G 33M .0G % /home
/dev/sdb5 .0G 6.0M .8G % /disk5
/dev/sdb1 976M 2.6M 907M % /disk1
/dev/sda1 197M 114M 83M % /boot
tmpfs 98M 98M % /run/user/
[root@localhost ~]# df -h|sed '2i This is added words'
文件系统 容量 已用 可用 已用% 挂载点
This is added words
/dev/sda5 16G .4G 15G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda2 .0G 33M .0G % /home
/dev/sdb5 .0G 6.0M .8G % /disk5
/dev/sdb1 976M 2.6M 907M % /disk1
/dev/sda1 197M 114M 83M % /boot
tmpfs 98M 98M % /run/user/
[root@localhost ~]# df -h|sed '2i This is \added words'
文件系统 容量 已用 可用 已用% 挂载点
This is dded words
/dev/sda5 16G .4G 15G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda2 .0G 33M .0G % /home
/dev/sdb5 .0G 6.0M .8G % /disk5
/dev/sdb1 976M 2.6M 907M % /disk1
/dev/sda1 197M 114M 83M % /boot
tmpfs 98M 98M % /run/user/
[root@localhost ~]# df -h|sed '2i This is \
> added words'
文件系统 容量 已用 可用 已用% 挂载点
This is
added words
/dev/sda5 16G .4G 15G % /
devtmpfs 479M 479M % /dev
tmpfs 489M 489M % /dev/shm
tmpfs 489M 6.8M 482M % /run
tmpfs 489M 489M % /sys/fs/cgroup
/dev/sda2 .0G 33M .0G % /home
/dev/sdb5 .0G 6.0M .8G % /disk5
/dev/sdb1 976M 2.6M 907M % /disk1
/dev/sda1 197M 114M 83M % /boot
tmpfs 98M 98M % /run/user/
[root@localhost ~]# sed '2c deleted...' testfile
no. name sex score age comment
deleted...
li:si f member
wangwu m teacher
[root@localhost ~]# sed '2c deleted...\
> new lines!!!' testfile
no. name sex score age comment
deleted...
new lines!!!
li:si f member
wangwu m teacher
[root@localhost ~]# cat testfile
no. name sex score age comment
zhansan m student
li:si f member
wangwu m teacher [root@localhost ~]# sed '3s/li:si/lisi/g' testfile 注释:2表示行序号,不加数字表示替换整个文档
no. name sex score age comment
zhansan m student
lisi f member
wangwu m teacher
[root@localhost ~]# cat testfile
no. name sex score age comment
zhansan m student
li:si f member
wangwu m teacher
[root@localhost ~]# sed -i '3s/li:si/lisi/g' testfile 注释:-i选项表示修改原文件,此选项有一定风险性,慎用,如果需要修改文件内容建议使用vim
[root@localhost ~]# cat testfile
no. name sex score age comment
zhansan m student
lisi f member
wangwu m teacher
[root@localhost ~]# sed -e '2s/zhansan/sanzhan/g;3s/li/Li/g' testfile
no. name sex score age comment
sanzhan m student
Lisi f member
wangwu m teacher