linux——sed,awk命令详情

sed的基础操作

命令格式

sed的命令格式: sed [options] 'command' file;
sed的脚本格式: sed [options] -f scriptfile file;

sed选项

-e: 在命令行模式上进行sed动作编辑,为默认选项;当给出多个指令时需要用-e选项,告诉sed将下一个参数解释为指令
-f: 将sed的动作写在文件中,用-f filename 执行文件内动作
-i: 直接修改文件内容
-n: 只打印模式匹配的行
-r: 支持扩展表达式

指定简单的指令

sed [-e] 'instruction' filename #单引号可以换! , #等。 根据实际情况使用

三种方式指定多重命令

  1. 用分号分隔指令
[root@node3 ~]# sed 's/tom/TOM/;s/jerry/JERRY/' test
  1. 在每个指令前放置-e
[root@node3 ~]# sed -e 's/tom/TOM/' -e 's/jerry/JERRY/' test
  1. 使用Bourne shell的分行指令功能
[root@node3 ~]# sed '
> s/tom/TOM/
> s/jerry/JERRY/
> ' test

sed命令

s 替换指定字符

[root@node3 ~]# cat test 
one two three
test redhat next
tom jerry jake

123 310 ABC
[root@node3 ~]# sed 's/jake/mike/' test
one two three
test redhat next
tom jerry mike

123 310 ABC

a 在当前行下插入文本

#在文件test行下插入文本
[root@node3 ~]# sed '/test/a\first' test
one two three
test redhat next
first
tom jerry jake

123 310 ABC

# 在文件第二行下插入文本
[root@node3 ~]# sed '2a\first' test
one two three
test redhat next
first
tom jerry jake

123 310 ABC

i 在当前行的上一行插入文本

[root@node3 ~]# sed '2i\second' test
one two three
second
test redhat next
tom jerry jake

123 310 ABC


[root@node3 ~]# sed '/test/i\first' test
one two three
first
test redhat next
tom jerry jake

123 310 ABC

n 下一行
在这里插入图片描述

[root@node3 ~]# cat test 
one two three
test redhat next
tom jerry jake

123 310 ABC
[root@node3 ~]# sed '/one/{n;s/next/time/}' test
one two three
test redhat time
tom jerry jake

123 310 ABC
[root@node3 ~]# cat test 
one two three
test redhat next
tom jerry jake

123 310 ABC

N 将当前读入的行和下一行看成“一行”
在这里插入图片描述

[root@node3 ~]# cat test 
one two three
test redhat next
tom jerry jake

123 310 ABC
[root@node3 ~]# sed '/next/N;s/next\ntom/computer/' test
one two three
test redhat computer jerry jake

123 310 ABC

d 删除

#删除空白行
[root@node3 ~]# cat test 
one two three
test redhat next
tom jerry jake

123 310 ABC
[root@node3 ~]# sed '/^$/d' test
one two three
test redhat next
tom jerry jake
123 310 ABC

#删除第二行
[root@node3 ~]# sed '2d' test
one two three
tom jerry jake

123 310 ABC
[root@node3 ~]# 

D 多行删除

[root@node3 ~]# cat test
one two three

test redhat next


tom jerry jake



123 310 ABC

# 多行Delete命令原理:当遇到两个空行,只删除第一个空行。当一个空行后面跟有文本,模式空间可以正常输出
[root@node3 ~]# sed '                    
>/^$/{
>N
>/^\n$/D
>} ' test
one two three

test redhat next

tom jerry jake

123 310 ABC

h 将模式空间的内容拷贝到保持空间,原来模式空间里的内容清除
H 将模式空间的内容拷贝到保持空间\n后
g 将保持空间内容拷贝到模式空间,原来的模式空间里的内容清除
G 将保持空间的内容复制到模式空间\n后

# 将one这一行内容放入保持空间,然后覆盖掉模式空间的test这一行
[root@node3 ~]# sed '/one/h;/test/g' test
one two three
one two three
tom jerry jake
123 310 ABC

# 将one这一行放入到保持空间,然后追加到test行后
[root@node3 ~]# sed '/one/h;/test/G' test
one two three
test redhat next
one two three
tom jerry jake
123 310 ABC

# 将1这一行放入保持空间后删除,追加到模式空间中
[root@node3 ~]# cat test 
11
22
111
222

[root@node3 ~]# sed '/1/{h;d};/2/G' test
22
11
222
111

x 表示交换莫板块中的文本和缓冲区中的文本


[root@node3 ~]# cat test 
1
2
3
4
5
# 匹配2到保持空间然后删除模式空间内容,匹配3到保持空间然后交换保持空间和模式空间的内容
[root@node3 ~]# sed '
/2/{
h
d
}
/3/{
x
}
' test
1
2
4
5

流控制命令

流控制命令两个用于控制脚本的哪一部分以及何时执行的命令。分支(b)和测试(t)命令将脚本中的控制转移到包含特殊标签的行,如果没有指定标签,则将控制转移到脚本的结尾处,分支命令用于无条件转移,测试命令用于有条件转移,他们只有当替换命令改变当前时才会执行

标签是任意不多于7个字符的序列,标签本身占据一行并以冒号开头:

:mylabel

在冒号和标签之间不允许有空格,行结尾处的空格将被认为是标签的一部分。当在分支命令或测试命令中指定标签时,在命令和标签之间允许有空格

b mylabel

分支 b 在脚本中将控制转移到另一行

[root@node3 ~]# cat test 
hello china
hello world
hello china world

#匹配china,继续匹配hello world,当不匹配其中一个时,进入下一个命令
[root@node3 ~]# cat txt 
:top
/china/{
/hello world/b top
s/china/CHINA/
}
[root@node3 ~]# sed -f txt test 
hello CHINA
hello world
hello CHINA world

#匹配hell,继续匹配hell world,当匹配正确时调转回top
[root@node3 ~]# cat txt 
:top
/hello/{
/hello world/b top
s/china/CHINA/
}
[root@node3 ~]# sed -f txt test 
hello CHINA

awk

awk是一个强大的文本分析工具,简单来说awk九十八文件逐行读入,(空格,制表符)为默认分隔符将每行切片,切开的部分再进行各种分析处理

awk命令格式

awk [-F field-separator] 'commands' input-file

[-F 分隔符]是可选的,因为awk使用空格,制表符作为缺省的字段分隔符,因此如果要浏览字段间有空格,制表符的文本,不必指定这个选项,但如果要浏览诸如/etc/passwd文件,此文件各字段以冒号作为分隔符,则必须指明-F选项

内置变量

FS 设置输入字段分隔符,等价于命令行-F选项
NF 浏览记录的字段个数
NR 已读的记录数

实例

# 以:为分隔符,显示第一列和第七列
[root@node3 ~]# awk -F ':' '{print $1,$7}' passwd 
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
mysql /bin/nologin

#打印行号,列数和对应的完整内容
[root@node3 ~]# awk -F ':' '{print NR,NF,$0}' passwd 
1 7 root:x:0:0:root:/root:/bin/bash
2 7 bin:x:1:1:bin:/bin:/sbin/nologin
3 7 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 7 mysql:x:975:973::/home/mysql:/bin/nologin

#指定特定分隔符,查询倒数第二列
[root@node3 ~]# awk -F ":" '{print $(NF-1)}' passwd 
/root
/bin
/sbin
/home/mysql

#显示文本以tab分割
[root@node3 ~]# awk -F ":" '{print $1"\t"$7}' passwd 
root	/bin/bash
bin	    /sbin/nologin
daemon	/sbin/nologin
mysql	/bin/nologin

#测试整数,字符还是空行
[root@node3 ~]# awk '/^$/{print "this is a blank line"};/[0-9]+/{print "this is a number"}' 
1
this is a number

this is a blank line

# 过滤ip
[root@node3 ~]# ip a |grep 'inet' | grep "ens160" | grep -v '127.0.0.1' | awk -F "[ /]+" '{print $3}'
192.168.8.132

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值