Linux三剑客-进阶必会

三剑客

Linux三剑客是指三个非常强大的命令行工具:grep、sed 和 awk。这些工具是Linux系统管理员和开发者经常使用的工具,能够在处理文本文件和数据流时提供极大的便利。

使用环境;Ubuntu2404

grep

grep用于在文件中搜索符合正则表达式的行,并输出包含匹配的行。

基础用法

基本语法

bash 复制代码
grep [选项] PATTERN [文件...]

常用选项:

  • -i:忽略大小写
  • -r:递归搜索目录中的文件
  • -v:反向匹配,显示不包含匹配的行
  • -n:显示匹配行的行号
  • -l:仅列出包含匹配的文件名
bash 复制代码
root@huhy:~# cat test
fjisjfeis
sfnods
hello
sfjeois
fsojfids
efjisofe
Hello

搜索包含"example"的行

bash 复制代码
grep "example" filename.txt
bash 复制代码
root@huhy:~# grep hello test
hello

忽略大小写搜索

bash 复制代码
grep -i "example" filename.txt
bash 复制代码
root@huhy:~# grep -i hello test
hello
Hello

递归搜索目录中的所有文件

bash 复制代码
grep -r "example" /path/to/directory
bash 复制代码
root@huhy:~# ls /opt/
test  test2
root@huhy:~# grep -r hello /opt/
/opt/test2:hello
/opt/test:hello

反向匹配:反向搜索内容

bash 复制代码
grep -v "example" filename.txt
bash 复制代码
root@huhy:~# grep -v hello /opt/test
fjisjfeis
sfnods
sfjeois
fsojfids
efjisofe
Hello

显示行号

bash 复制代码
grep -n "example" filename.txt
bash 复制代码
root@huhy:~# grep -n hello /opt/test
3:hello

高级用法

进阶必会;正则表达式,基本上三剑客使用难度就在于正则表达式的运用

正则表达式(Regular Expression,简称regex)是一种用来匹配字符串的模式。它在文本处理工具中非常强大且灵活。下面是正则表达式的详细用法,涵盖基本字符、元字符、字符类、量词和分组等方面。

基本字符

  • a:匹配字符'a'
  • abc:匹配字符串"abc"

元字符

  • .:匹配除换行符外的任意一个字符
  • ^:匹配字符串的开头
  • $:匹配字符串的结尾
  • \:转义字符,用来匹配特殊字符本身

字符类

  • [abc]:匹配'a'、'b'或'c'中的任意一个字符
  • [^abc]:匹配除'a'、'b'或'c'之外的任意一个字符
  • [a-z]:匹配小写字母'a'到'z'中的任意一个字符
  • [A-Z]:匹配大写字母'A'到'Z'中的任意一个字符
  • [a-Z]:匹配小写字母'a'到大写'Z'中的任意一个字符
  • [0-9]:匹配数字'0'到'9'中的任意一个字符

量词

  • *:匹配前面的字符零次或多次
  • +:匹配前面的字符一次或多次
  • ?:匹配前面的字符零次或一次
  • {n}:匹配前面的字符恰好n次
  • {n,}:匹配前面的字符至少n次
  • {n,m}:匹配前面的字符至少n次,至多m次

分组和引用

  • ():将匹配的字符分组
  • |:匹配左侧或右侧的字符
  • \n:引用分组的内容(n是分组的序号,从1开始)

特殊字符类

  • \d:匹配数字,等价于[0-9]
  • \D:匹配非数字字符,等价于[^0-9]
  • \w:匹配字母、数字或下划线字符,等价于[a-zA-Z0-9_]
  • \W:匹配非字母、数字或下划线字符,等价于[^a-zA-Z0-9_]
  • \s:匹配空白字符(包括空格、制表符、换页符等),等价于[ \t\r\n\v\f]
  • \S:匹配非空白字符,等价于[^ \t\r\n\v\f]

匹配包含数字的行

bash 复制代码
grep '[0-9]' filename.txt
bash 复制代码
root@huhy:~# cat test
djfosdjfe
fjowejfofne end
start disonfeow
943 sjfowj
jfowejr304j fjeow
jfodaie cat dfjsoedog;sdj
root@huhy:~# grep '[0-9]' test
943 sjfowj
jfowejr304j fjeow

匹配以"start"开头的行

bash 复制代码
grep '^start' filename.txt
bash 复制代码
root@huhy:~# grep '^start' test
start disonfeow

匹配以"end"结尾的行

bash 复制代码
grep 'end$' filename.txt
bash 复制代码
root@huhy:~# grep 'end$' test
fjowejfofne end

匹配包含"cat"或"dog"的行(扩展正则表达式)

bash 复制代码
grep -E 'cat|dog' filename.txt
bash 复制代码
root@huhy:~# grep -E 'cat|dog' test
jfodaie cat dfjsoedog;sdj
root@huhy:~#

常用场景

可组合命令使用;例如过滤ip a命令中包含192的内容

bash 复制代码
root@huhy:~# ip a | grep 192
    inet 192.168.200.190/24 brd 192.168.200.255 scope global ens33
root@huhy:~#

过滤文档中包含注释符的内容;使用-E选项来启用扩展正则表达式

bash 复制代码
root@huhy:~# cat test
#djfosdjfe
#fjowejfofne end
#start disonfeow
#943 sjfowj
jfowejr304j fjeow
jfodaie cat dfjsoedog;sdj
root@huhy:~# grep -Ev "^$|#" test
jfowejr304j fjeow
jfodaie cat dfjsoedog;sdj

sed

一种流编辑器,用于过滤和转换文本

基本用法

基本语法

bash 复制代码
sed [选项] '命令' 文件

常用选项:

  • -e:添加执行的脚本
  • -i:直接修改文件
  • -n:取消默认输出

常用命令:

  • s/regexp/replacement/:替换匹配的文本
  • d:删除匹配的行
  • p:打印匹配的行
bash 复制代码
root@huhy:~# cat demo
apple
orange
dkcis
djfisf
example
apple
orange

将文件中所有的"apple"替换为"orange"

bash 复制代码
sed 's/apple/orange/g' filename.txt
bash 复制代码
root@huhy:~# sed 's/apple/orange/g' demo
orange
orange
dkcis
djfisf
example
orange
orange

直接在文件中进行替换

bash 复制代码
sed -i 's/orange/apple/g' filename.txt
bash 复制代码
root@huhy:~# sed -i 's/orange/apple/g' demo
root@huhy:~# cat demo
apple
apple
dkcis
djfisf
example
apple
apple

删除包含"example"的行

bash 复制代码
sed '/example/d' filename.txt
bash 复制代码
root@huhy:~# cat demo
apple
apple
dkcis
djfisf
example
apple
apple
root@huhy:~# sed '/example/d' demo
apple
apple
dkcis
djfisf
apple
apple

打印包含"apple"的行

bash 复制代码
sed -n '/apple/p' filename.txt
bash 复制代码
root@huhy:~# sed -n '/apple/p' demo
apple
apple
apple
apple

高级用法

默认使用基本正则表达式,可以使用-r或-E选项启用扩展正则表达式

替换所有数字为"#"

bash 复制代码
sed 's/[0-9]/#/g' filename.txt
bash 复制代码
root@huhy:~# cat test
#djfosdjfe
#fjowejfofne end
#start disonfeow
#943 sjfowj
jfowejr304j fjeow
jfodaie cat dfjsoedog;sdj
root@huhy:~# sed 's/[0-9]/#/g' test
#djfosdjfe
#fjowejfofne end
#start disonfeow
#### sjfowj
jfowejr###j fjeow
jfodaie cat dfjsoedog;sdj

删除包含"start"的行

bash 复制代码
sed '/start/d' filename.txt
bash 复制代码
root@huhy:~# cat test
#djfosdjfe
#fjowejfofne end
#start disonfeow
#### sjfowj
jfowejr###j fjeow
jfodaie cat dfjsoedog;sdj
root@huhy:~# sed '/start/d' test
#djfosdjfe
#fjowejfofne end
#943 sjfowj
jfowejr304j fjeow
jfodaie cat dfjsoedog;sdj
root@huhy:~#

替换包含"cat"或"dog"的行(扩展正则表达式)

bash 复制代码
sed -E 's/(cat|dog)/animal/g' filename.txt
bash 复制代码
root@huhy:~# sed -E 's/(cat|dog)/animal/g' test
#djfosdjfe
#fjowejfofne end
#start disonfeow
#943 sjfowj
jfowejr304j fjeow
jfodaie animal dfjsoeanimal;sdj

常用场景:删除指定行数的内容

删除特定行

bash 复制代码
sed '2d' example.txt

删除多个特定行

bash 复制代码
sed '2d; 4d' example.txt

删除行范围

bash 复制代码
sed '2,4d' example.txt

删除从某行到文件末尾的内容

bash 复制代码
sed '3,$d' example.txt

#删除包含特定模式的行

bash 复制代码
sed '/banana/d' example.txt

常用场景;替换指定行数的内容

替换特定行的内容

bash 复制代码
sed '2s/.*/test/' example.txt

替换多个特定行的内容

bash 复制代码
sed '2s/.*/test/; 4s/.*/test/' example.txt

替换行范围的内容

bash 复制代码
sed '2,4s/.*/test/' example.txt

awk

一种编程语言,用于在文本文件中基于模式进行扫描和处理

基本用法

基本语法

bash 复制代码
awk '脚本' 文件

常用选项:

  • -F:指定字段分隔符

常用内置变量:

  • NR:记录的行号
  • NF:字段的个数
  • $0:当前行的全部文本
  • $n:第n个字段

打印文件的每一行

awk '{print $0}' filename.txt

bash 复制代码
root@huhy:~# cat huhy
a a a a a a a
b b b b b b
c c c c c
root@huhy:~# awk '{print $0}' huhy
a a a a a a a
b b b b b b
c c c c c

打印文件的第一列

awk '{print $1}' filename.txt

bash 复制代码
root@huhy:~# awk '{print $1}' huhy
a
b
c

打印包含"example"的行的行号和内容

bash 复制代码
awk '/example/ {print NR, $0}' filename.txt
bash 复制代码
root@huhy:~# awk '/b/ {print NR, $0}' huhy
2 b b b b b b

高级用法

默认使用扩展正则表达式

打印包含数字的行

bash 复制代码
awk '/[0-9]/ {print}' filename.txt
bash 复制代码
root@huhy:~# cat test
dsfe89697sefs
startdifnewi33
jodsofeined end
root@huhy:~# awk '/[0-9]/ {print}' test
dsfe89697sefs
startdifnewi33

打印以"start"开头的行

bash 复制代码
awk '/^start/ {print}' filename.txt
bash 复制代码
root@huhy:~# awk '/^start/ {print}' test
startdifnewi33

打印以"end"结尾的行

bash 复制代码
root@huhy:~# awk '/^start/ {print}' test
startdifnewi33
bash 复制代码
awk '/end$/ {print}' filename.txt
bash 复制代码
root@huhy:~# awk '/end$/ {print}' test
jodsofeined end

常用场景

用于和命令搭配使用;例如IP

bash 复制代码
root@huhy:~# ip a | awk '/inet / {print $2} /inet6 / {print $2}'
127.0.0.1/8
::1/128
192.168.200.190/24
fe80::20c:29ff:fe5f:fb01/64
fe80::20c:29ff:fe5f:fb0b/64
root@huhy:~#
相关推荐
Tak1Na10 分钟前
2024.9.18
linux·运维·服务器
A^mber14 分钟前
828华为云征文|云服务器Flexus X实例|Ubunt部署Vue项目
运维·服务器·华为云
安得权19 分钟前
Ubuntu 20.04 部署 NET8 Web - Systemd 的方式 达到外网访问的目的
linux·前端·ubuntu
让学习成为一种生活方式25 分钟前
解析药用植物重楼甾体皂苷生物合成中的连续糖基化及其抗真菌作用-文献精读49
linux·数据库·算法·天然产物化学
凯哥是个大帅比1 小时前
ubuntu20.04 GLIBC从2.35降级到2.31
linux
iHero1 小时前
【Ubuntu】在 Ubuntu 22.04.3 LTS 安装 davfs2 通过 Nextcloud WebDAV 挂载到 Ubuntu 的目录上
linux·ubuntu·nextcloud
清园暖歌1 小时前
Ubuntu 不重装系统增加交换空间大小
linux·运维·ubuntu·交换空间
黎相思1 小时前
操作系统迁移(CentOs -> Ubuntu)
linux·ubuntu·gitee·centos
写bug如流水1 小时前
在Ubuntu 20.04上安装pgAdmin 4
linux·运维·ubuntu
冰红茶兑滴水1 小时前
Linux 线程控制
linux·c++·算法