linux文本编辑三剑客
文章目录
- grep常用的用法
- awk常用的用法
- sed常用的用法
- 追加
- 插入
- 更改
- 转换
文本处理三剑客: | 功能 |
---|---|
grep | 过滤文本或文件 |
awk | 针对列,筛选类容 |
sed | 针对行,编辑文件内容 |
grep常用的用法
正常用法
[root@localhost ~]# grep 'hello world' abc
hello world
忽略大小写
[root@localhost ~]# grep -i 'hello world' abc
hello world
HELLO WORLD
取反
[root@localhost ~]# grep -v 'hello world' abc
hellllo
helllo
hellol
静默模式,不输出任何结果
[root@localhost ~]# grep -q 'hello world' abc
[root@localhost ~]# echo $?
0
[root@localhost ~]# grep -q 'hello asddfsadfasdf' abc
[root@localhost ~]# echo $?
1
’
-A 1 //被模式匹配到的内容以及其后面一行的内容都显示出来
-B 1 //被模式匹配到的内容以及其前面一行的内容都显示出来
-C 1 //被模式匹配到的内容以及其前后的行各显示1行
[root@localhost ~]# grep -B 1 'helllo' abc
hellllo
helllo
[root@localhost ~]# grep -A 1 'helllo' abc
helllo
hellol
[root@localhost ~]# grep -C 1 'helllo' abc
hellllo
helllo
hellol
加入正则表达式
//加’‘就是正则表达式,使用正则表达式的方式
[root@localhost ~]# grep 'hell..' abc
helllo
hellol
[root@localhost ~]# grep 'hell..$' abc //$ 结束
helllo
hellol
[root@localhost ~]# grep '^h' abc //^ 开头
hellllo
helllo
hellol
[root@localhost ~]# grep '^$' abc //取出空行[root@localhost ~]# grep -v '^$' abc //取反,除了空行的
hellllo
helllo
hellol
awk常用的用法
例1
取出分隔符后的内容
//'{print $1":"$7}' 打印1列和7列
// -F: 用 :号当分割符
[root@localhost ~]# head -3 passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# awk -F: '{print $1,$7}' passwd | head -3
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
[root@localhost ~]# awk -F: '{print $1":"$7}' passwd | head -3
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
[root@localhost ~]# awk -F: '{print $1"\t"$7}' passwd | head -3
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
例2
取出行列的内容
//awk默认分隔符是空格,无论多少空格都算一个空格
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/cs-root 17G 1.8G 16G 11% /
/dev/nvme0n1p1 1014M 210M 805M 21% /boot
tmpfs 371M 0 371M 0% /run/user/0
//打印第4列的内容
[root@localhost ~]# df -h|awk '{print $4}'
可用
1.8G
1.9G
1.9G
1.9G
16G
805M
371M
//打印第6行的内容,第4列的内容
//NR==6 判断行==第6行
[root@localhost ~]# df -h|awk 'NR==6{print $4}'
16G
//打印最后列的内容
//$NF 最后一列
[root@localhost ~]# df -h|awk '{print $NF}'
挂载点
/dev
/dev/shm
/run
/sys/fs/cgroup
/
/boot
/run/user/0//打印倒数第2列
[root@localhost ~]# df -h|awk '{print $(NF-1)}'
已用%
0%
0%
1%
0%
11%
21%
0%
//打印除了第一列的倒数第2列
[root@localhost ~]# df -h|awk 'NR!=1{print $(NF-1)}'
0%
0%
1%
0%
11%
21%
0%
//查看文件有几列
[root@localhost ~]# df -h|awk '{print NF}'
6
6
6
6
6
6
6
6
//查看文件有几行
[root@localhost ~]# df -h|awk '{print NR}'
1
2
3
4
5
6
7
8
sed常用的用法
//注意效果图我没有截全,只截了改变的部分
常用sed参数 | 用法 |
---|---|
-n | 表示显示处理后的结果 |
-i | 直接编辑文本文件 |
-E | 使用扩展正则表达式 |
命令 | 意思 |
---|---|
a | 在后面新增一行 |
i | 在前面插入一行 |
d | 是删除模式空间类容 |
D | 是删除模式空间的第一行 |
p | 打印当前模式空间内容 |
P | 打印当前模式空间开端至\n的内容,并追加到默认输出之前 |
n | 命令是提前读取下一行,覆盖模式空间前一行 |
N | 命令是追加下一行到模式空间,同时将两行看做一行 |
g | 命令是将当前保持空间中内容覆盖至模式空间 |
G | 命令是将当前保持空间中的内容追加至模式空间 |
h | 命令是将当前模式空间中内容覆盖至保持空间 |
H | 命令是将当前模式空间中的内容追加至保持空间 |
x | 命令是交换保持空间和模式空间的内容 |
打印3到7行
[root@localhost ~]# sed -n '3,7p' anaconda-ks.cfg
删除3到7行
sed -n '3,7d' anaconda-ks.cfg
准备测试文件
[root@localhost ~]# cat > test <<EOF
> heel heell woool
> world hello
> hehe eheehe qj
> passwd: 123445
> passwd:123456
> EOF
追加
在1行后面追加test
[root@localhost ~]# sed '1atest' test
在1行后面追加test,添加3个空格
// \ 转义
[root@localhost ~]# sed '1a \ test' test
heel heell woooltest
在找到的字符后面追加test
[root@localhost ~]# sed '/^world/a test' test
heel heell woool
world hello
test
准备测试文件
[root@localhost ~]# vi test
[root@localhost ~]# cat test
heel heell woool
world hello
hehe eheehe qj
passwd: 123445
passwd:123456
item 14.04
item 8.13
item 13.3
item 8.8
在找到的字符后面的数字后面追加test
[root@localhost ~]# sed '/^item.*[0-9].[0-9]/a test' test
item 14.04
test
item 8.13
test
item 13.3
test
item 8.8
test
在找到的字符后面的是俩位数字的后面追加test
[root@localhost ~]# sed '/^item.*[0-9][0-9].[0-9]/a test' test
item 14.04
test
item 8.13
item 13.3
test
item 8.8
插入
在第2行插入
[root@localhost ~]# sed '2itest' test
heel heell woool
test
在所选的字符前面插入test
[root@localhost ~]# sed '/^hehe/i test' test
heel heell woool
world hello
test
更改
将第2行的内容改查test
[root@localhost ~]# sed '2c test' test
heel heell woool
test
在所选的字符行改成test
[root@localhost ~]# sed '/^hehe/c test' test
heel heell woool
world hello
test
在所选的字符行改成test前面添加3个空行
[root@localhost ~]# sed '/^hehe/c \ test' test
heel heell woool
world hellotest
在所选的字符行改成test前面添加3个空行,换行添加一行hehe
更改一行为多行
[root@localhost ~]# sed '/^hehe/c \ test\nhehe' test
heel heell woool
world hellotest
hehe
以world开头,445结尾,以及中间部分改成test
更改多行为一行
[root@localhost ~]# sed '/^world/,/445$/c test' test
heel heell woool
test
passwd:123456
转换
转换可以更改字符,但是只能是数量一样的字符
准备测试文件
[root@localhost ~]# sed '2y/world/tests/' test
heel heell woool
tests hette
hehe eheehe qj
[root@localhost ~]# cat test
heel heell woool
world hello
hehe eheehe qj
passwd: 123445
passwd:123456
更改第2行world为tests
[root@localhost ~]# sed '2y/world/tests/' test
heel heell woool
tests hette
hehe eheehe qj
passwd: 123445
passwd:123456
linux文本编辑三剑客相关推荐
- linux 匹配文本中的ip,linux文本三剑客匹配网卡IP地址大PK(CentOS 7系统)
运维工程师在做配置的过程中很多时候都需要去获取目标服务器网卡上的IP地址,那究竟用什么方式获取更便捷了,博主今天就带大家使用linux文本三剑客分别获取一下网卡的IP地址,最后我们再来对比一下. 实验 ...
- Linux文本编辑工具
Linux文本编辑工具 前言 一.常用的Linux文本编辑工具 二.各类工具详解 1.vim 2.cat 3.rev 把同一行内容逆向显示 4.head 5.tail 6.tr命令 7.cut命令 L ...
- Linux文本三剑客--grep、sed、awk
Linux文本三剑客–grep.sed.awk 文章目录 Linux文本三剑客--grep.sed.awk 1.grep 1.1什么是grep 1.2使用grep 命令格式 命令参数 基本正则表表达式 ...
- linux文本三剑客演讲稿,linux文本三剑客之grep
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...
- linux文本编辑利器-vim
概述 Vim是从vi发展出来的一个文本编辑器.代码补全.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用,和Emacs并列成为类Unix系统用户最喜欢的文本编辑器.Vim强大的编辑能力中很 ...
- Linux文本编辑器之vim
博客荒废了好久,最近在一本书中看到这样一段话,"再普通的事件一经记录就成为历史,一成为历史就将价值连城".虽然我正在做的课后笔记远远谈不上是历史,但于准备学以致用的我而言,这种记录 ...
- Linux文本三剑客(grep、sed、awk)
目录 grep 1.什么是grep和rgrep? 2.使用grep 2.1.命令格式 2.2.命令功能 2.3.命令参数 3.实战演示 sed 1.认识sed 2.使用sed 2.1.命令格式 2.2 ...
- Linux文本三剑客
Linux下文本三剑客 正则表达式 文本三剑客Grep 文本三剑客Sed 文本三剑客Awk awk.grep.sed是linux操作文本的三大利器,合称文本三剑客,也是必须掌握的linux命令之一.三 ...
- linux:三剑客老三grep
linux三剑客老三:grep linux中常用的文本(awk,sed,grep)处理工具之一 首先谈一下grep命令的常用格式为:grep [选项] "模式" [文件] grep ...
最新文章
- Linux内核设计第五周学习总结 分析system_call中断处理过程
- MyEclipse修改jsp模板文件
- javaSocket与C通信
- php页面上限制字符串长度,PHP如何限制字符串显示长度
- Qt Designer设置背景图片、颜色不影响其它组件小技巧,控件层级设置,组件的继承,styleSheet设置样式。
- BFD (双向转发检测) 协议简介与开发
- react 逆地理 高德地图_在react中使用原生的高德地图
- IDEA使用技巧整理
- ieda中快捷搜索_快捷指令(07)早上好(三)播报当日日程安排。
- 栈--进栈,出栈指针修改的顺序问题
- 聊聊你们关心的视频号
- 一般什么时候加骨窗_高三成绩大幅度提高一般出现在什么时候?老师道出了实情...
- python批量获取百度贴吧_python网络爬虫案例:批量爬取百度贴吧页面数据
- iOS输入框搜索节流输入框输入拼音的问题 (防抖)
- 忘记了已保存自动登录的密码,怎么办?
- Visio绘制维恩图举例
- 计算机13E怎么转换成十进制,十六进制换算(进制转换计算器)
- oled电视和量子点电视哪个好 oled电视与量子点电视区别
- mini2440 -- TFTP方式下载u-boot
- python列表中的索引问题:从左到右由0开始;从右到左由-1开始