加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

grep、sed、awk三剑客

发布时间:2020-12-14 00:21:51 所属栏目:Linux 来源:网络整理
导读:grep 对文本内容进行过滤 用法: 1.grep -i 不区分大小写 [[email?protected] ~]# echo -e " hello worldnHELLO world " | grep - i hellohello worldHELLO world[[email?protected] ~]# echo -e " hello worldnHELLO world " | grep hellohello world 2.g

grep

对文本内容进行过滤

用法:

1.grep -i  不区分大小写

[[email?protected] ~]# echo -e "hello worldnHELLO world"|grep -i hello
hello world
HELLO world
[[email?protected] ~]# echo -e "hello worldnHELLO world"|grep hello
hello world

2.grep -n  显示行号

[[email?protected] ~]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

3.grep -o  只打印匹配的内容

[[email?protected] ~]# echo "hello world"|grep -o hello
hello
[[email?protected] ~]# echo "hello world"|grep hello
hello world

4.grep -c  只打印匹配的行数

[[email?protected] ~]# grep -c root /etc/passwd
2
[[email?protected] ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

5.grep -v  打印不匹配的行

[[email?protected] ~]# echo -e "hello worldnyou are beautiful"|grep -v hello
you are beautiful
[[email?protected] ~]# echo -e "hello worldnyou are beautiful"|grep hello
hello world

6.grep -w  精确匹配

[[email?protected] ~]# echo -e "hello worldnhellow world"|grep hello
hello world
hellow world
[[email?protected] ~]# echo -e "hello worldnhellow world"|grep -w hello
hello world

sed

一个非交互式编辑工具

用法:

增删改查

增
a        append 行后追加
i        insert 行前插入
1 sed "2a hello" test 或 sed 2a"hello" test 
2 sed "2i hello" test
3 sed "2i hellonworldnbeautiful" test  #第二行前插入多行

删 d delete 删除
1 删除指定行 sed 2d test 2 删除指定范围行 sed 2,5d test 3 删除匹配的行 sed /sixth/d test 4删除指定行到末行的内容 sed 2,$d test 5 取反(只保留) sed 2,3!d test #只保留第2,3行 sed /tenth/!d test #只保留包含tenth的行 sed /root/!d /etc/passwd 等于 grep root /etc/passwd 改 c change 替换 sed 2c hello test #替换第二行 sed软件替换模型 1 sed s/line/hang/g test 2 sed -i s/line/hang/g test 命令说明:如果想真正的修改文件内容,我们就需要使用选项“-i”,这个要和sed命令“i”区分开来。同时我们可以发现命令执行后的结果是没有任何输出的。
查 p     print 打印 输出指定内容,但默认会输出2次匹配的结果,因此使用
-n选项取消默认输出 sed ‘2p‘/etc/passwd #显示文件的全部内容,第2行会显示两次 sed -n ‘2p‘/etc/passwdsed 2!d /etc/passwd #只显示第二行 sed -n /ninth/p test #只显示包含ninth的行 -e多点操作 sed -e 2d -e 5d test #同时删除第2和5行 sed -n -e 2p -e 5p test #只显示第2和5行

awk

获取需要的行列信息

概念:

-F:指定分隔符

记录(record):一行就是一个记录

分隔符(field separator):进行对记录进行切割的时候所使用的字符

字段(field):将一条记录分割成的每一段

FILENAME:当前处理文件的文件名

FS(Field Separator):字段分隔符(默认是以空格为分隔符=)

NR(Number of Rrecord):记录的编号(awk每读取一行,NR就加1==)

NF(Number of Field):字段数量(记录了当前这条记录包含多少个字段==)

ORS(Output Record Separator):指定输出记录分隔符(指定在输出结果中记录末尾是什么,默认是n,也就是换行)

OFS(Output Field Separator):输出字段分隔符

RS:记录分隔符

用法:

$1 $2 ... $n?输出一个指定的字段(列) ?第1列、第2列、第n

$NF?输出最后一个字段

$0?输出整条记录

1 打印所有内容并显示行号
awk {print NR,$0} /etc/passwd
2 输出超过5个字段的行的第三个字段    
awk -F : NF>=5{print $3} /etc/passwd
3 输出每行行号和该行有几个字段
awk -F : {print NR,NF} /etc/passwd
4匹配行首为root
awk /^root/ /etc/passwd
5 匹配行尾为sync
awk -F : /sync$/ /etc/passwd
6 匹配一行中的某一列
awk -F : $5~/root/ /etc/passwd        #匹配第5列为root的行
7显示第四行
awk NR==4 /etc/passwd
8 在结果前打印 hello,可以是任何的字符
awk BEGIN{print "hello"}{print $0} /etc/passwd
9 在结果后打印 hello,可以是任何的字符
awk END{print "hello"}{print $0} /etc/passwd
10 使用awk进行计算 +加 -减 *乘 /除 可以带() ^开方
awk BEGIN{print 2*3} 
11 比较 >大于 <小于 >=大于等于 <=小于等于 ==等于
awk -F ":" $3<=88{print $1,$3} /etc/passwd

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读