格式化输出

printf:格式化输出命令

%ns:输出字符串。n是数字指代输出几个字符
%ni:输出整数。n是数字指代输出几个数字
%m.nf:输出浮点数。m和n是数字,指代输出的整数位数和小数位数。如%8.2f 代表共输出8位数,其中2位是小数,6位是整数。

输出格式:
a:输出警告声音
:输出退格键,也就是Backspace键
f:清除屏幕
:换行
:回车,也就是Enter键
:水平输出退格键,也就是Tab键
v:垂直输出退格键,也就是Tab键

[root@localhost tmp]# printf '%s %s %s ' 1 2 3 4 5 6
1 2 3
4 5 6

[root@localhost tmp]# cat student.txt
ID Name gender mark
1 sl M 89
2 hus M 90
3 sd M 99
[root@localhost tmp]# printf '%s %s %s %s ' $(cat student.txt)
ID Name gender mark
1 sl M 89
2 hus M 90
3 sd M 99

awk '条件1{动作1} 条件2{动作2}...' 文件名

条件:
x>10 判断变量x是否大于10
x>=10 大于等于
x<=10 小于等于
动作:
格式化输出
流程控制语句

[root@localhost tmp]# cat student.txt
ID Name gender mark
1 sl M 89
2 hus M 90
3 sd M 99
[root@localhost tmp]# awk '{printf $2 " " $4" " }' student.txt
Name mark
sl 89
hus 90
sd 99
说明:$2 代表第二列
$4 代表第四列

[root@localhost tmp]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 3.2G 14G 19% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@localhost tmp]# df -h | awk '{print $1 " " $5 " " $6}'
文件系统 已用% 挂载点
/dev/mapper/centos-root 19% /
devtmpfs 0% /dev
tmpfs 0% /dev/shm
tmpfs 2% /run
tmpfs 0% /sys/fs/cgroup
/dev/sda1 14% /boot
tmpfs 0% /run/user/0
说明:awk可以根据空格截取


截取/dev/sda1的百分比
[root@localhost tmp]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 3.2G 14G 19% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0

[root@localhost tmp]# df -h | grep /dev/sda1
/dev/sda1 1014M 133M 882M 14% /boot

[root@localhost tmp]# df -h | grep /dev/sda1 | awk '{print $5}'
14%

[root@localhost tmp]# df -h | grep /dev/sda1 | awk '{print $5}' | cut -d "%" -f 1
14

BEGIN 用法:
在执行输出第二列第四列之前,先输出 test!!
[root@localhost tmp]# awk 'BEGIN{print "test!!"} {print $2 " " $4}' student.txt
test!!
Name mark
sl 89
hus 90
sd 99

FS内置变量,分隔符:第一列没有分割
[root@localhost tmp]# awk '{FS=":"} {print $1 " " $3}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
nobody 99
systemd-network 192
dbus 81
polkitd 999
sshd 74
postfix 89
mysql 27
nginx 998
chrony 997
zhangsan 1000
user1 1001
user2 1002
user3 1003

添加BEGIN,第一列分割
[root@localhost tmp]# awk 'BEGIN{FS=":"} {print $1 " " $3}' /etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
nobody 99
systemd-network 192
dbus 81
polkitd 999
sshd 74
postfix 89
mysql 27
nginx 998
chrony 997
zhangsan 1000
user1 1001
user2 1002
user3 1003

END命令,在所有的命令执行后,在执行:
[root@localhost tmp]# awk 'END{print "The end!!"}{print $1 " " $3}' student.txt
ID gender
1 M
2 M
3 M
The end!!

sed命令:轻量级流编辑器。主要用来将数据进行选取、替换、删除、新增

sed [选项]'[动作]' 文件名
选项:
-n:一般sed命令会把所有数据都输出到屏幕,如果加入此选择,则只会把经过sed命令处理的行输出到屏幕。
-e:允许对输入数据应用多条sed命令编辑
-i:用sed的修改结果直接修改读取数据的文件,而不是由屏幕输出

动作:
a: 追加,在当前行后添加一行或多行。添加多行时,除最后一行外 ,每行末尾需要用“”代表数据未完结。
c: 行替换,用c后面的字符串 替换原数据行,替换多行时,除最后一行外,每行末尾需用“”代表数据未完结。
i: 插入,在当前行前插入一行或多行。插入多行时,除最后一行外,每行末尾需要用“”代表数据未完结。
d: 删除,删除指定的行。
p: 打印,输出指定的行
s: 字符串替换 ,用一个字符串替换另一个字符串。格式“行范围s/旧字串/新字串/g”

输出文件第二行:
[root@localhost tmp]# cat student.txt
ID Name gender mark
1 sl M 89
2 hus M 90
3 sd M 99
[root@localhost tmp]#

原文件也会在下面输出
[root@localhost tmp]# sed '2p' student.txt
ID Name gender mark
1 sl M 89
1 sl M 89
2 hus M 90
3 sd M 99

原文件不会在下面输出
[root@localhost tmp]# sed -n '2p' student.txt
1 sl M 89

配合管道符
[root@localhost tmp]# df -h | sed -n '2p'
/dev/mapper/centos-root 17G 3.2G 14G 19% /

删除第2行到第4行:
[root@localhost tmp]# cat student.txt
ID Name gender mark
1 sl M 89
2 hus M 90
3 sd M 99
[root@localhost tmp]# sed '2,4d' student.txt
ID Name gender mark

在第二行后追加hello
[root@localhost tmp]# sed '2a hello' student.txt
ID Name gender mark
1 sl M 89
hello
2 hus M 90
3 sd M 99

在第二行前插入两行数据:
[root@localhost tmp]# sed '2i hello ihao' student.txt
ID Name gender mark
hello
ihao
1 sl M 89
2 hus M 90
3 sd M 99

第四行替换为 no person
[root@localhost tmp]# sed '4c no person' student.txt
ID Name gender mark
1 sl M 89
2 hus M 90
no person

第三行中90换成10
[root@localhost tmp]# sed '3s/90/10/g' student.txt
ID Name gender mark
1 sl M 89
2 hus M 10
3 sd M 99

替换完保存到文件:
[root@localhost tmp]# sed -i '3s/90/10/g' student.txt
[root@localhost tmp]# cat student.txt
ID Name gender mark
1 sl M 89
2 hus M 10
3 sd M 99

把sl和sd替换为空
[root@localhost tmp]# sed -e 's/sl//g;s/sd//g' student.txt
ID Name gender mark
1 M 89
2 hus M 10
3 M 99

原文地址:https://www.cnblogs.com/javasl/p/11155152.html