【linux相识相知】sed命令

2023-11-01 12:40
文章标签 linux 命令 sed 相识 相知

本文主要是介绍【linux相识相知】sed命令,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在之前的博客中我们介绍了文本三剑客中grep,本次博客就另外一名剑客——sed做出详细的描述,sed真的是一款强大的工具。下面让我们来一起看一下吧!

 

概述和工作机制

SED的英文全称为Stream EDitor,中文称流编辑器。默认情况下,它会一行一行的读取文件中的内容,在了解其工作原理之前,首先我们得先知道一下几个概念:

1.模式空间(pattern buffer):sed从文件中读取行首先会放到模式空间中进行执行和处理,定义的sed命令都是在这里执行的,默认情况下会逐行的读,逐行的处理,除非你做了行定界。

2.保持空间(hold buffer):在处理完模式空间的一些行的时候,我们有可能需要一个临时的地方去存放模式空间中的内容,这时候就可以将模式空间中的内容放到保持空间了。

初始情况下,模式空间和保持空间都是空的。

1.默认情况下,将从文件中逐行的读取内容至模式空间;

2.默认情况下,模式空间中的内容在处理完成后将会打印到标准输出;

3.sed命令在模式空间中的都是按顺序执行的,除非指定了行定界,否则将在所有的行上面执行;

4.修改后的行被送至标准输出的之后,模式空间将被清空。

 

基本选项

 语法格式:

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

-n:不输出模式空间中的内容至标准输出

#例子1
[root@localhost ~]# sed '' /etc/fstab #
# /etc/fstab
# Created by anaconda on Wed Sep  6 11:16:57 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=0ab24855-5180-4d3e-a61d-99ca54711c2c /boot                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
[root@localhost ~]# 
#例子2
[root@localhost ~]# sed -n '' /etc/fstab 
[root@localhost ~]#

默认情况下,sed命令会逐行的读取文件中每一行内容至模式空间,再执行执行单引号内的命令,如例子1,单引号内没有任何内容,所以不会对行内容作任何的处理,所以sed会逐行读取文件的内容,然后逐行的显示到标准输出,每打印一行到标准输出,模式空间就会被清空一次。在例子2中,加了-n的选项,会阻止输出到标准输出,所以就不会显示任何的内容。

 -e script:指定多个命令

[root@localhost ~]# cat poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain
#例子3
[root@localhost ~]# sed -e '1d' -e '10d' poetry 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
11)Know it will pass,  
12)And strength you will gain 

使用-e选项,可以同时对行作出多个模式匹配,d的意思是删除。这里的例子3,文件poetry是输入,sed读取第一行的内容至模式1空间,然后判断是否是第一行,判断成功是第一行,则删除第一行(从模式空中清除,也不会打印至标准输出),读取第二行,先判断是否是第一行,不是,再判断是否是第十行,不是,然后打印至标准输出,所以在标准输出中只显示了不包含第一行和第十行的其他行的内容。注意:sed不会修改原文件的内容。

-f  /path/to/script:把sed的编辑命令放在文件中,注意每一行一个命令:

#例子4
[root@localhost ~]# cat sed_script.txt    #编辑命令存放的文本文件
1d
10d
[root@localhost ~]# sed -f sed_script.txt poetry 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
11)Know it will pass,  
12)And strength you will gain 

我们可以看到例子4中sed使用-f选项指定编辑命令所在的文件,执行的是和例子3同样的操作,删除第一行和第十行,当我们得编辑命令有很多,把它放在文件也是一个不错的做法吧!

 

行定界

默认情况下不指定行的定界,会读取处理整个文件的每一行。如果指定了行,还是会读取每一行,但是在执行各种命令操作之后,会进行判断是不是定界的行,如果判断成功,则执行操作,如删除行,如果判断失败,则不做任何的处理然后打印至标准输出,下面来看一下几种常见的定界方法:

(1)定界为空,对全文的每一行进行处理

[root@localhost ~]# sed  '' poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
(2)单行指定
a.直接写单个数字,表示指定行
[root@localhost ~]# sed  '2d' poetry  #当判断为第二行的时候,会删除第二行
1)Never give up,  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain

b./pattern/:被模式匹配到的行,默认可以是基本的正在表达式

[root@localhost ~]# sed  '/As/ d' poetry  #删除被模式匹配的行
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

c.$最后一行

[root@localhost ~]# sed  '$ d' poetry  #删除最后一行
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,

(3)定界范围

a.x,y:x至y行

[root@localhost ~]# sed  '1,9 d' poetry  #删除了1-9行
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

b.x,+y:x行到,x行往下数y个行

[root@localhost ~]# sed  '1,+3 d' poetry #删除第一行,和第一行往下的3行,也就是一直删除到第四行
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

c.x,/pattren/:x到被模式匹配的行

[root@localhost ~]# sed  '2,/smile/ d' poetry  #删除第二行至被smile匹配的行,smile被第九行匹配
1)Never give up,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

d./pattren1/,/pattern2/:被模式1匹配的行开始到被模式2匹配的行

[root@localhost ~]# sed '/Always/,/put/ d' poetry 
1)Never give up,  
2)Never lose hope.  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
(4)使用"~"指定步进
"~"号前面为开始行,后面为步进的长度:
[root@localhost ~]# sed '1~2 d' poetry  #显示偶数行
2)Never lose hope.  
4)It allows you to cope.  
6)As they always do.  
8)Your dreams will come true.  
10)You'll live through your pain.  
12)And strength you will gain 
[root@localhost ~]# sed '2~2 d' poetry   #显示奇数行
1)Never give up,  
3)Always have faith,  
5)Trying times will pass,  
7)Just have patience,  
9)So put on a smile,  
11)Know it will pass, 

 

编辑命令

下面我们来看一下编辑命令,如前面的d选项是删除行的意思,那么除了d选项,还有哪些呢?

p:显示模式空间的内容

#例5
[root@localhost ~]# sed   'p' poetry 
1)Never give up,  
1)Never give up,  
2)Never lose hope.  
2)Never lose hope.  
3)Always have faith,  
3)Always have faith,  
4)It allows you to cope.  
4)It allows you to cope.  
5)Trying times will pass,  
5)Trying times will pass,  
6)As they always do.  
6)As they always do.  
7)Just have patience,  
7)Just have patience,  
8)Your dreams will come true.  
8)Your dreams will come true.  
9)So put on a smile,  
9)So put on a smile,  
10)You'll live through your pain.  
10)You'll live through your pain.  
11)Know it will pass,  
11)Know it will pass,  
12)And strength you will gain 
12)And strength you will gain 
#例6
[root@localhost ~]# sed -n  'p' poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
View Code

在例5中,每一行都被打印了2次,原因是在模式空间的时候被打印了一次,然后处理完成之后,在标准输出又被打印了一次,所以自然会显示出两次。如果使用-n,则仅仅打印的模式空间的内容。

a test:在行后面追加文本"test",可以使用\n实现多行追加

[root@localhost ~]# sed '1 a hi i am here\nhey ' poetry 
1)Never give up,  
hi i am here
hey 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

i test:在 行前面插入文本test,支持使用\n实现多行插入

[root@localhost ~]# sed '2i  hi i am here\nhey ' poetry 
1)Never give up,  
hi i am here
hey 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

c test:把匹配到的行替换为此处指定的行

[root@localhost ~]# sed '2c  hi i am here ' poetry 
1)Never give up,  
hi i am here 
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

w /path/to/somefile:保存模式空间中匹配到的内容到指定的文件中

[root@localhost ~]# sed '/So/  w  /tmp/poetry' poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
[root@localhost ~]# 
[root@localhost ~]# cat /tmp/poetry 
9)So put on a smile,  

r /path/from/somefile:读取指定文件的内容至当前文件中被模式匹配的行后面

[root@localhost ~]# cat poetry2.txt 
13)yes! I can!
[root@localhost ~]# sed '12 r poetry2.txt' poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
13)yes! I can!

=:为模式匹配到的行打印行号,打印在行的上面

[root@localhost ~]# sed '/Never/ =' poetry
1
1)Never give up,  
2
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

!:条件取反

[root@localhost ~]# sed '/Never/ !d' poetry  #不删除被默认匹配的行
1)Never give up,  
2)Never lose hope.  

s///:查找替换,分割符可以自定义,比如使用s@@@flag或者s###flag,默认会替换被匹配的第一个位置,但是可以添加替换标记:

s///g:全局替换

[root@localhost ~]# cat poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
[root@localhost ~]# sed 's/\<you\>/your/g' poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows your to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength your will gain 
View Code

sed默认支持的基本的正则表达式,使用-r, --regexp-extended选项可以支持扩展的正则表达式。

 

练习1:删除/boot/grub2/grub.cfg文件中所有以空白字符开头的行的行首的所有空白字符

[root@localhost ~]# sed -r 's@^[[:space:]]+@@g' /boot/grub2/grub.cfg
View Code

练习2:删除/etc/fstab文件中所有以#开头的行的行首的#号及#后面的所有空白字符

[root@localhost ~]# sed 's/^#[[:space:]]*//g' /etc/fstab
View Code

练习3:输出一个目录给sed,取出其目录,类似于dirname

[root@localhost ~]# echo "/var/log/messages" | sed -r 's@[^/]+/?$@@'
/var/log/
[root@localhost ~]# 
[root@localhost ~]# echo "/var/log/messages/" | sed -r 's@[^/]+/?$@@'
/var/log/
View Code

 

 高级编辑命令

 sed还有一些高级的命令,会用到之前说到的保持空间。

 

举一些例子来看一下吧!

[root@localhost ~]# cat poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

可以自己先思考一下答案,再点开:

[root@localhost ~]# sed -n 'n;p' poetry  #只显示偶数行
2)Never lose hope.  
4)It allows you to cope.  
6)As they always do.  
8)Your dreams will come true.  
10)You'll live through your pain.  
12)And strength you will gain 
sed -n 'n;p' poetry
[root@localhost ~]# sed  '1!G;h;$!d'  poetry  #逆序显示
12)And strength you will gain 
11)Know it will pass,  
10)You'll live through your pain.  
9)So put on a smile,  
8)Your dreams will come true.  
7)Just have patience,  
6)As they always do.  
5)Trying times will pass,  
4)It allows you to cope.  
3)Always have faith,  
2)Never lose hope.  
1)Never give up, 
sed '1!G;h;$!d' poetry
[root@localhost ~]# sed  '$!d'  poetry  #只保留最后一行,类似tail -1
12)And strength you will gain 
sed '$!d' poetry
[root@localhost ~]# sed  '$!N;$!D' poetry  #取出最后两行
11)Know it will pass,  
12)And strength you will gain 
sed '$!N;$!D' poetry
[root@localhost ~]# sed 'G' poetry  #在每一行的后面添加一个空白行
1)Never give up,  2)Never lose hope.  3)Always have faith,  4)It allows you to cope.  5)Trying times will pass,  6)As they always do.  7)Just have patience,  8)Your dreams will come true.  9)So put on a smile,  10)You'll live through your pain.  11)Know it will pass,  12)And strength you will gain 
sed 'G' poetry

这些都是一些高级的用法,在工作中用的可能比较少,因为有的结果完全可以用其他简单的命令去实现。

 

循环和分支

 sed最牛逼之处莫过于这个了,sed提供了一个循环和分支工来控制程序的执行流程。

 循环

sed循环的工作原理类似于现代编程语言中的goto语句。

定义sed的标签:

:label

跳转到标签的位置,使用b命令后面跟着标签名即可,下面我们来看一个具体的实例:

[root@localhost ~]# cat teleplay 
剧名:白夜追凶
评分:9.0
剧名:秘密深林
评分:9.3
剧名:权利的游戏第七季
评分:9.3
剧名:请回答1988
评分:9.6
[root@localhost ~]# sed -n '
h;n;H;x
s@\n@,@
/请回答/!b label
s@^@---@
:label
p' teleplay
剧名:白夜追凶,评分:9.0
剧名:秘密深林,评分:9.3
剧名:权利的游戏第七季,评分:9.3
---剧名:请回答1988,评分:9.6

分析:

由于命令很长,我们可以将命令分行来写,当然也可以写在一行里面:

1.h;n;H;x:将第一行内容读取到模式空间,执行h,将模式空间的内容覆盖至保持空间,执行n,从文件中读取第二行覆盖至模式空间,执行H,将模式空间的内容追加至保持空间,执行x,将保持空间的内容和模式空间调换;(这里得到的结果就是模式空间中存在两行);

2.s@\n@,@,将换行符替换为逗号;这样原来的两行就变成了一行;

3./请回答/!b label,判断这一行中是否有“请回答”三个字符,如果没有则直接跳到":label",然后执行打印"p",打印模式空间中的内容,如果有则执行"s@^@---@",将在行首添加字符"---"。

这样一来,只有"剧名:请回答1988,评分:9.6"被匹配,然后进行了行首的替换操作。

 

分支

 可以使用t来创建分支。使用t命令跳转到指定的标签,一样我们来看一个例子:

[root@localhost ~]# sed -n '
> h;n;H;x
> s/\n/,/
> :loop
> /白夜追凶/s/^/-/
> /-----/!t loop
> p' teleplay
-----剧名:白夜追凶,评分:9.0
剧名:秘密深林,评分:9.3
剧名:权利的游戏第七季,评分:9.3
剧名:请回答1988,评分:9.6

分析:
由于命令很长,我们可以将命令分行来写,当然也可以写在一行里面:

1:h;n;H;x:将第一行内容读取到模式空间,执行h,将模式空间的内容覆盖至保持空间,执行n,从文件中读取第二行覆盖至模式空间,执行H,将模式空间的内容追加至保持空间,执行x,将保持空间的内容和模式空间调换;(这里得到的结果就是模式空间中存在两行);

2:s@\n@,@,将换行符替换为逗号;这样原来的两行就变成了一行;

3::loop:定义了一个loop标签;

4:/白夜追凶/s/^/-/:是否匹配模式,如果匹配则在其行首添加一个"-";如果不匹配,则直接打印;

5:/-----/!t loop:是否存在5个"-",如果不存在,则跳到标签loop处,继续执行第4步添加"-",直到满足5个"-",则跳出循环打印。

最后,我们之前的所有的操作,都是没有修改文件的本身的,可以使用 -i 选项来直接修改文件本身,慎用,建议在使用之前,先备份一下文件!

[root@localhost ~]# sed -i '1d' poetry
[root@localhost ~]# 
[root@localhost ~]# cat poetry
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

总结:sed的确是一个强大的文本处理工具,功能非常丰富,需要在今后的日常使用和工作中不断的熟悉和巩固。

 

参考链接:http://blog.jobbole.com/109088/     《三分钟学会SED》写的非常好!

 

转载于:https://www.cnblogs.com/liubinsh/p/7516402.html

这篇关于【linux相识相知】sed命令的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/323059

相关文章

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

Linux查询服务器 IP 地址的命令详解

《Linux查询服务器IP地址的命令详解》在服务器管理和网络运维中,快速准确地获取服务器的IP地址是一项基本但至关重要的技能,下面我们来看看Linux中查询服务器IP的相关命令使用吧... 目录一、hostname 命令:简单高效的 IP 查询工具命令详解实际应用技巧注意事项二、ip 命令:新一代网络配置全