文本处理工具(二)
目录
一、sed工具
1、sed介绍?
<#> 什么是sed?
答:sed是一种流编辑器,它通过一次处理一行内容,把该行的内容放入缓存空间(也称模式空间)中,然后对该行进行处理,处理完后后再发送到终端!
<#> 为什么要用sed?
答:使用sed可以对大型文件进行内容的增删改查操作,且十分方便!
2、sed的增删改查
<#> sed 语法:
sed 选项参数 匹配条件/条件匹配后要执行的sed命令 filename
注释:
1、sed会一行行的读取文件内容存储到缓存,再和设置的匹配条件进行匹配,如果匹配成功,执行后面的sed处理数据命令,如果不成功,跳过sed命令!
2、当没有设置匹配条件时,文件所有行都会执行sed命令
<#> 选项参数:
-e: # 可以执行多条sed命令
-i: # 加-i表示直接修改源文件
-f: #
-n: # 只输出匹配成功操作执行后的结果内容
-r:
<#> sed命令参数:
a add新增,在匹配行的后一行新增数据 (不改变源文件,只是预览)
i insert插入,向文件匹配行前插入内容 (不改变源文件,只是预览)
c change修改,修改文件匹配行的内容
d delete删除,删除文件匹配的内容
P print打印,打印匹配行内容,常与选项-n使用
s substitute替换,替换匹配行内容
= 用来打印被匹配的行的行号
n 读取下一行,遇到n时自动跳到下一行
<#> 常用特殊符号
! 取反
{sed命令1;sed命令2} 多个操作命令操作同一匹配行内容
2.1、sed—增
<#> sed增删改查操作:
增:
1、<在指定行前面新增内容>
sed 选项参数 '指定行/指定内容i要增加的内容' 文件名
sed -i '1ihellworld' sed.txt # 在文件第一行后新增一行hellworld
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '1ahellworld' sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
2、<在指定行后面面新增内容>
sed 选项参数 '指定行/指定内容a要增加的内容' 文件名
sed '1ahellworld' sed.txt # 在文件第一行前新增一行hellworld
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '1ahellworld' sed.txt
name
hellworld
age abc
29 30
ASD
sex
birthplace
marriage
number
2.2、sed—删
删:
1、<删除指定行内容>
sed 选项参数 'nd' 文件名
sed '2d' sed.txt # 删除文件sed.txt的第二行内容
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '2d' sed.txt
name
29 30
ASD
sex
birthplace
marriage
number
2、<隔行删除内容>
sed 选项参数 '1~2d' 文件名
sed '1~2d' sed.txt # 从第1行开始,隔2行进行删除
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '1~2d' sed.txt
age abc
ASD
birthplace
number
3、<删除第1行到第2行的内容>
sed 选项参数 '1,2d' 文件名
sed '1,2d' sed.txt
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '1,2d' sed.txt
29 30
ASD
sex
birthplace
marriage
number
4、<除了第1到第2行外全部删除>
sed 选项参数 '1,2!d' 文件名
sed '1,2!d' sed.txt
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '1,2!d' sed.txt
name
age abc
5、<删除最后一行>
sed 选项参数 '$d' 文件名
sed '$d' sed.txt
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '$d' sed.txt
name
age abc
29 30
ASD
sex
birthplace
marriage
6、<删除匹配行到最后一行,包含匹配行>
sed 选项参数 '/[0-9]/,$d' 文件名
sed '/[0-9]/,$d' sed.txt # 删除数字行到最后一行
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '/[0-9]/,$d' sed.txt
name
age abc
7、<删除匹配行到后面n行,包含匹配行>
sed 选项参数 '/[0-9]/,+nd' 文件名
sed '/[0-9]/,+2d' sed.txt # 删除数字行到后2行
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '/[0-9]/,+2d' sed.txt
name
age abc
birthplace
marriage
number
8、<删除除匹配行除外的内容, *和|需要转义 >
sed 选项参数 '/[0-9]\|[A-Z]/!d' 文件名
sed '/[0-9]\|[A-Z]/!d' sed.txt # 删除除数字行和大写行外的所 有内容
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '/[0-9]\|[A-Z]/!d' sed.txt
29 30
ASD
2.3、sed—改
改:
# 注意:
修改(c): 修改全部匹配的内容行
替换(s): 默认只替换一行中的第一个
1、<修改指定行的内容>
sed 选项参数 '指定行c要修改的内容' 文件名
sed '2chelloworld' sed.txt # 把第2行修改为helloworld
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '2chelloworld' sed.txt
name
helloworld
29 30
ASD
sex
birthplace
marriage
number
2、<修改全部匹配行的内容>
sed 选项参数 '指定行/指定内容c要修改的内容' 文件名
sed '/[0-9]/chelloworld' sed.txt # 把数字行修改为helloworld
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '/[0-9]/chelloworld' sed.txt
name
age abc
helloworld
ASD
sex
birthplace
marriage
3、<修改最后一行的内容>
sed 选项参数 '指定行/指定内容c要修改的内容' 文件名
sed '$chelloworld' sed.txt # 把最后一行修改为helloworld
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '$chelloworld' sed.txt
name
age abc
29 30
ASD
sex
birthplace
marriage
helloworld
4、<替换匹配行第一个的内容>
sed 选项参数 's/匹配内容/要替换的内容' 文件名
sed 's/[0-9]/hello/' sed.txt # 如果一行中有多个数字内容,默认 替换第一个数字内容为hello
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed 's/[0-9]/hello/' sed.txt
name
age abc
hello9 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed 's/[0-9]/hello/1' sed.txt
name
age abc
hello9 30
ASD
sex
birthplace
marriage
number
5、<替换所有匹配到的内容>
sed 选项参数 's/指定内容/要替换的内容/g' 文件名
sed 's/[0-9]/hello/g' sed.txt # 替换所有数字内容为hello
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed 's/[0-9]/hello/g' sed.txt
name
age abc
hellohello hellohello
ASD
sex
birthplace
marriage
number
6、<替换一行中符合匹配条件的第n个内容>
sed 选项参数 's/指定内容/要替换的内容/n' 文件名
sed 's/[0-9]/hello/2' sed.txt # 替换一行中符合数字匹配条件 的第2行数字为hello
# 演示
[root@localhost home]# cat sed.txt
name
age
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed 's/[0-9]/hello/2' sed.txt
name
age abc
2hello 30
ASD
sex
birthplace
marriage
number
7、<替换后的内容写入文件>
方式一、
sed -n 's/设置匹配条件/要替换的内容/2pw sed1.txt' sed.txt
sed -n 's/[0-9]/hello/2pw sed1.txt' sed.txt
# 把能匹配成功的每行的第2个数字替换成hello,并把替换后的内容写入 到sed1.txt中
-n 表示只输出匹配后操作成功的结果内容
-p 只打印匹配后操作成功的结果内容,与-n一起用
-w 写入
# 演示
[root@localhost home]# cat sed.txt
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed 's/[0-9]/hello/2pw sed2.txt' sed.txt
name
age abc
2hello 30
2hello 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# cat sed2.txt
2hello 30
方式二、
sed -n 's/设置匹配条件/要替换的内容/2p' sed.txt > sed1.txt
sed -n 's/[0-9]/hello/2p' sed.txt > sed1.txt
# 演示
[root@localhost home]# cat sed.txt
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed -n 's/[0-9]/hello/2p' sed.txt > sed1.txt
[root@localhost home]# cat sed1.txt
2hello 30
8、<正则表达式匹配替换>
sed -n '/i/s/t.*/hello/p' sed.txt
# 把每行含i字符的t字符后面的所有字符替换为hello
# 演示
[root@localhost home]# cat sed.txt
name
age
29
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed -n '/s/s/e.*/hello/p' sed.txt
shello
9、<每行末尾拼接txt>
sed 's/$/& txt' sed.txt
# &是拼接的意思! 在每行末尾拼接一个.txt
# 演示
[root@localhost home]# cat sed.txt
name
age
29
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed 's/$/&.txt/' sed.txt
name.txt
age.txt
29.txt
ASD.txt
sex.txt
birthplace.txt
marriage.txt
number.txt
10、<每行行首添加注释 # >
sed 's/^/&#/' sed.txt
# 演示
root@localhost home]# cat sed.txt
name
age
29
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed 's/^/&#/' sed.txt
#name
#age
#29
#ASD
#sex
#birthplace
#marriage
#number
2.4、sed—查
查:
1、<查询含有指定内容的行 >
sed -n '/指定内容/p' sed.txt
sed -n '/hell/p' sed.txt
# 查询含hell的内容并打印出来
# 演示
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed -n '/hello/p' sed.txt
[root@localhost home]# sed -n '/hell/p' sed.txt
hellworld
2、<管道过滤查询>
ps -aux |sed -n '/sshd/p'
# 查询所有进程中含sshd的进程信息
# 演示
[root@localhost home]# ps -aux |sed -n '/sshd/p'
root 1146 0.0 0.1 112924 4324 ? Ss 12月03 0:00 /usr/sbin/sshd -D
root 15207 0.0 0.0 117040 980 pts/0 S+ 00:41 0:00 sed -n /sshd/p
3、<多个sed命令一起执行>
sed -e '2d' -e 's/hell/world/g' sed.txt
等同于
sed '2d;s/hell/world/g' sed.txt
#演示
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed -e '2d' -e 's/hell/world/g' sed.txt
worldworld
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '2d;s/hell/world/g' sed.txt
worldworld
age abc
29 30
ASD
sex
birthplace
marriage
number
3、sed的缓存区的数据交换
1、 <sed的缓存区有两个:>
1、模式空间: sed会先从文件中读取一行,然后存入模式空间,然后进行相应处理!是读一行处理一行!
2、暂存空间:暂存空间刚开始里面只有一个空行;模式空间与暂存空间可以进行数据之间的相互流转
2、< 缓存区的sed程序命令 >
h 模式空间——>暂存空间(数据复制,覆盖)
H 模式空间——>暂存空间(数据复制,追加)
g 暂存空间——>模式空间(数据复制,覆盖)
G 暂存空间——>模式空间(数据复制,追加)
x 交换两个空间的内容
3.1 sed缓存区数据转换的使用
1、<将模式空间的第一行复制到暂存空间(覆盖),然后将暂存空间的内容复制到模式空间的最后一行(追加)>
sed '1h;$G' sed.txt
# 演示
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '1h;$G' sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
hellworld
2、<将模式空间的第一行复制到暂存空间(覆盖)并删除,然后将暂存空间的内容复制到模式空间的最后一行(追加)>
sed '1h;1d;$G' sed.txt
# 演示
[root@localhost home]# sed '1h;1d;$G' sed.txt
name
age abc
29 30
ASD
sex
birthplace
marriage
number
hellworld
[root@localhost home]# sed '1{h;d};$G' sed.txt
name
age abc
29 30
ASD
sex
birthplace
marriage
number
hellworld
3、<将模式空间的第一行复制到暂存空间(覆盖),然后将暂存空间的内容复制到模式空间中替换从第2行开始到最后一行的每一行数据(覆盖)>
sed '1h;2,$g' sed.txt
# 演示
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '1h;2,$g' sed.txt
hellworld
hellworld
hellworld
hellworld
hellworld
hellworld
hellworld
hellworld
hellworld
4、< 将前3行数据复制到暂存空间(追加),然后将暂存空间的所有内容复制粘贴到模式空间的最后一行(追加)>
sed '1,3H;$G' sed.txt
# 演示
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed '1,3H;$G' sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
hellworld
name
5、< 给每一行添加空格 >
sed -i G sed.txt
# 演示
[root@localhost home]# sed -i G sed.txt
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
6、< 删除所有的空行 >
sed -i '/^$/d' sed.txt
# 演示
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed -i '/^$/d' sed.txt
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
二、awk工具
1、awk的简单介绍
# 是一个强大的文本分析工具,可自定义变量、运算、流程控制等方式进行文本分析,灵活简单!
2、awk的语法、参数、变量
# 语法:
awk 选项参数 '匹配条件{操作命令}' {文件名}
# 选项参数:
-F 指定输入文件拆分分隔符
-v 设置一个变量
# awk的内置变量
FILENAME 指当前被awk使用的文件名
NR 指行号
NF 指列号
3、awk的使用
1、< 打印字符串拼接输入 >
echo 'a b c' |awk '{print $1":"$2":"$3 }'
#输出:
[root@localhost home]# echo 'a b c' |awk '{print $1":"$2":"$3 }'
a:b:c
[root@localhost home]# echo 'a b c'|awk '{print $1':'$2':'$3}'
awk: cmd. line:1: {print $1:$2:$3}
awk: cmd. line:1: ^ syntax error
2、< 打印含有匹配信息的行 >
awk '/匹配条件/' 文件名
[root@localhost home]# awk '/world/' sed.txt
hellworld
3、< 打印匹配行中第n列的数据 >
awk -F: '/匹配关键字/{print $n}' 文件名
awk -F: '/boy/{print $7}' passwd1
# 以boy为匹配关键,用:作分隔符切割截取第7列数据
[root@localhost home]# cat passwd1
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
gluster:x:996:993:GlusterFS daemons:/run/gluster:/sbin/nologin
saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
chrony:x:994:991::/var/lib/chrony:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
unbound:x:993:990:Unbound DNS resolver:/etc/unbound:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
sssd:x:992:988:User for sssd:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:991:987:User for geoclue:/var/lib/geoclue:/sbin/nologin
setroubleshoot:x:990:986::/var/lib/setroubleshoot:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial- setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi- daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
boy:x:1000:1000:boy:/home/boy:/bin/bash
[root@localhost home]# awk -F: '/boy/{print $7}' passwd1
/bin/bash
4、< 统计并输出文件中对应的:文件名、每行行号、每行行数、每行对应的内容 >
方式一、
awk -F: '{print "文件名:"FILENAME,"行号:"NR,"列数:"NF,"内容:"$0}' 文件名
[root@localhost home]# awk -F: '{print "filename:"FILENAME,"hang:"NR,"lie:"NF,"neirong:"$0}' passwd1
filename:passwd1 hang:1 lie:7 neirong:root:x:0:0:root:/root:/bin/bash
filename:passwd1 hang:2 lie:7 neirong:bin:x:1:1:bin:/bin:/sbin/nologin
filename:passwd1 hang:3 lie:7 neirong:daemon:x:2:2:daemon:/sbin:/sbin/nologin
filename:passwd1 hang:4 lie:7 neirong:adm:x:3:4:adm:/var/adm:/sbin/nologin
filename:passwd1 hang:5 lie:7 neirong:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
filename:passwd1 hang:6 lie:7 neirong:sync:x:5:0:sync:/sbin:/bin/sync
filename:passwd1 hang:7 lie:7 neirong:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
filename:passwd1 hang:8 lie:7 neirong:halt:x:7:0:halt:/sbin:/sbin/halt
filename:passwd1 hang:9 lie:7 neirong:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
filename:passwd1 hang:10 lie:7 neirong:operator:x:11:0:operator:/root:/sbin/nologin
filename:passwd1 hang:11 lie:7 neirong:games:x:12:100:games:/usr/games:/sbin/nologin
filename:passwd1 hang:12 lie:7 neirong:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
filename:passwd1 hang:13 lie:7 neirong:nobody:x:99:99:Nobody:/:/sbin/nologin
方式二、(printf函数)
awk -F: '{printf("filename:%s,hang:%s,lie:%s,neirong:%s\n",FILENAME,NR,NF,$0)}' passwd1
[root@localhost home]# awk -F: '{printf("filename:%s,hang:%s,lie:%s,neirong:%s\n",FILENAME,NR,NF,$0)}' passwd1
filename:passwd1,hang:1,lie:7,neirong:root:x:0:0:root:/root:/bin/bash
filename:passwd1,hang:2,lie:7,neirong:bin:x:1:1:bin:/bin:/sbin/nologin
filename:passwd1,hang:3,lie:7,neirong:daemon:x:2:2:daemon:/sbin:/sbin/nologin
filename:passwd1,hang:4,lie:7,neirong:adm:x:3:4:adm:/var/adm:/sbin/nologin
filename:passwd1,hang:5,lie:7,neirong:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
filename:passwd1,hang:6,lie:7,neirong:sync:x:5:0:sync:/sbin:/bin/sync
5、< 打印第二行信息>
awk -F: 'NR==3{print("filename:%s,%s\n",FILENAME,$0)}' passwd1
[root@localhost home]# awk -F: 'NR==3{print("filename:%s,%s\n",FILENAME,$0)}' passwd1
passwd1 daemon:x:2:2:daemon:/sbin:/sbin/nologin
6、< 查找当前目录下以b开头的文件/文件夹 >
ls -a |awk '/^b/'
[root@localhost home]# ls
boy passwd1 sed1.txt sed2.txt sed.txt test.sh xwh
[root@localhost home]# ls -a |awk '/^b/'
boy
7、< 打印第一列 >
awk -F: '{print $1}' passwd1
[root@localhost home]# awk -F: '{print $1}' passwd1
root
bin
daemon
adm
lp
sync
[root@localhost home]# awk -F: '{print $7}' passwd1
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
8、< 打印最后一列 >
awk -F: '{print $(NF-1)}' passwd
# NF-n 表示倒数第n列
[root@localhost home]# awk -F: '{print $(NF-1)}' passwd1
/root
/bin
/sbin
/var/adm
/var/spool/lpd
/sbin
/sbin
/sbin
[root@localhost home]# awk -F: '{print $(NF-2)}' passwd1
root
bin
daemon
adm
lp
sync
shutdown
9、< 打印倒数第二列 >
[root@localhost home]# awk -F: '{print $(NF-2)}' passwd1
root
bin
daemon
adm
lp
sync
shutdown
10、< 打印1-3行的第一列 >
awk -F: '{if(NR>=1 && NR<=3){print $1}}' passwd1
# awk 里面可以写逻辑控制表达式
[root@localhost home]# cat passwd1
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
[root@localhost home]# awk -F: '{if(NR>=1 && NR<=3){print $1}}'passwd1
root
bin
daemon
11、< 多分隔符使用 >
echo 'name:zhang/sna'|awk -F '[:/]' '{print $1$2$3}'
# 把字符串里的分隔符装到[]里, 注意''和''之间要空格
[root@localhost home]# echo 'name:zhang/sna'|awk -F '[:/]' '{print $1$2$3}'
namezhangsna
[root@localhost home]# echo 'name:zhang/sna'|awk -F '[:/]' '{print $1"+"$2"+"$3}'
name+zhang+sna
12、< 添加开始与结束内容 >
echo -e 'aaa\nbbb'| awk 'BEGIN{print "启动中..."}{print $0}END{print "结束中..."}'
# {}里最好使用"",不要用''
[root@localhost home]# echo -e 'aaa\nbbb'| awk
'BEGIN{print "启动中..."}{print $0}END{print "结束中..."}'
启动中...
aaa
bbb
结束中...
# 不然报错
[root@localhost home]# echo -e 'aaa\nbbb'| awk 'BEGIN{print '启动 中...'}{print $0}END{print '结束中...'}'
awk: cmd. line:1: BEGIN{print 启动中...}{print $0}END{print 结束中...}
awk: cmd. line:1: ^ invalid char '�' in expression
awk: cmd. line:1: BEGIN{print 启动中...}{print $0}END{print 结束中...}
awk: cmd. line:1: ^ syntax error
13、< awk可以无视多个空格分隔符,一样进行拼接 >
echo -e 'aaa nbbb'| awk -F" " '{print $1$2}'
# -F" " 无视多个连续空格一样进行拼接
[root@localhost home]# echo -e 'aaa nbbb'| awk -F" " '{print $1$2}'
aaanbbb
[root@localhost home]# echo -e 'aaa nbbb'| awk '{print $1$2}'
aaanbbb
15、< 使用循环拼接分割后的字符串 >
echo 'xie wen hui'| awk -v str='' '{for(n=1;n<NF;n++){str=str$n}} END{print str}'
# {str=str$n}这里千万不能用+拼接; {for(n=1;n<NF;n++){str=str$n}}这里是循环赋值;END{print str}最后打印拼接后的字符
[root@localhost home]# echo 'xie wen hui'| awk -v str='' '{for(n=1;n<=NF;n++){str=str$n}} END{print str}'
xiewenhui
[root@localhost home]# echo 'xie wen hui'| awk -v str='' '{for(n=1;n<=NF;++n){str=str$n}} END{print str}'
xiewenhui
16、< 操作指定数字运算 >
echo '22' | awk -v i=2 '{print $0+i}'
[root@localhost home]# echo '22' | awk -v i=2 '{print $0+i}'
24
17、< 切割ip >
[root@localhost home]# ifconfig | awk '/broadcast/'| awk 'NR==4{print$2}'
192.168.124.140
18、< 显示空行行号 >
[root@localhost home]# cat sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed ' G' sed.txt
hellworld
name
age abc
29 30
ASD
sex
birthplace
marriage
number
[root@localhost home]# sed 'G' sed.txt | awk '/^$/{print NR}'
2
4
6
8
10
12
14
16
18
# grep、sed、awk、cut作为文本字符串操作的四大工具之间的区别:
grep: 用于查找匹配的行
cut: 用于截取文件中列的字符,按分隔符切割提取,但不适合一行中有多个连续的空格
sed: 用于对文件中的行进行增删改查的操作
awk: 截取分析数据,可以通过行和列来查询操作文本字符,也可以处理连续的多个空白字符,非常强大!
浙公网安备 33010602011771号