1、cut字段提取命令
[root@localhost sh]# cat student.txt
ID Name gender Mark
1 dd M 90
2 drt M 98
3 go M 87
[root@localhost sh]# cut -f 2 student.txt
Name
dd
drt
go
[root@localhost sh]#
[root@localhost sh]# cut -f 2,4 student.txt
Name Mark
dd 90
drt 98
go 87
[root@localhost sh]#
[root@localhost sh]# cut -d ":" -f 1,3 /etc/passwd
[root@localhost sh]# cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1
2、printf格式化输出命令
[root@localhost sh]# printf %s 1 2 3 4 5 6
123456[root@localhost sh]# printf %s %s %s 1 2 3 4 5 6
%s%s123456[root@localhost sh]# printf '%s %s %s' 1 2 3 4 5 6
1 2 34 5 6[root@localhost sh]# printf '%s %s %s\n' 1 2 3 4 5 6
1 2 3
4 5 6
[root@localhost sh]#
3、awk命令
[root@localhost sh]# awk '{printf $2 "\t" $3 "\n"}' student.txt
[root@localhost sh]# df -h | awk '{print $1 "\t" $3}'
[root@localhost sh]# df -h | grep sda5 | awk '{print $5}' | cut -d "%" -f 1
[root@localhost sh]# awk 'BEGIN{printf "this is a transcript\n"} {printf $2 "\t" $3"\n"}' student.txt
[root@localhost sh]# cat /etc/passwd | grep "/bin/bash" | awk 'BEGIN {FS=":"} {printf $1 "\t" $3 "\n"}'
[root@localhost sh]# awk 'END{printf "the end \n"} {printf $2 "\t" $4 "\n"}' student.txt
[root@localhost sh]# cat student.txt | grep -v Name | awk '$4>=80 {printf $2 "\n"}'
4、sed命令
命令格式
动作
#查看文件第二行
[root@localhost sh]# sed -n '2p' student.txt
1 dd M 90
[root@localhost sh]# df -h | sed -n '2p'
/dev/sda5 16G 4.0G 11G 28% /
#删除第一到第三行的数据,但不修改文件本身
[root@localhost sh]# sed '1,3d' student.txt
3 go M 87
[root@localhost sh]# cat student.txt
ID Name gender Mark
1 dd M 90
2 drt M 98
3 go M 87
[root@localhost sh]#
在第二行后追加hello
[root@localhost sh]# sed '2a hello' student.txt
ID Name gender Mark
1 dd M 90
hello
2 drt M 98
3 go M 87
在第二行前插入两行数据
[root@localhost sh]# sed '2i hello \
> woeld' student.txt
ID Name gender Mark
hello
woeld
1 dd M 90
2 drt M 98
3 go M 87
[root@localhost sh]#
sed字符串替换
#sed ‘s/旧字串/新字串/g’ 文件名
在第三行中把98替换成89
[root@localhost sh]# sed '3s/98/89/g' student.txt
ID Name gender Mark
1 dd M 90
2 drt M 89
3 go M 87
sed操作的数据直接写入文件
[root@localhost sh]# sed -i '3s/98/89/g' student.txt
[root@localhost sh]# cat student.txt
ID Name gender Mark
1 dd M 90
2 drt M 89
3 go M 87
[root@localhost sh]#
同时把go和dd替换为空
[root@localhost sh]# sed -e 's/go//;s/dd//' student.txt