跳转至

2. 案例

1. 案例

1. 准备案例数据

cd /tmp
vim cp.txt

My name is chupeng.
I like linux.
I like playing computer game.
My qq is 747356101.
My wechat is cpu19900130.
My website is https://chupeng.site.

2.输出文件的第2,3行

#1. sed默认输出所有结果
sed '2,3p' cp.txt

#2. 加上-n参数则只输出第2,3行
sed -n '2,3p' cp.txt
sed -n '2,3'p cp.txt
#1.默认输出所有内容
root@baiduyun:/tmp# sed '2,3p' cp.txt
My name is chupeng.
I like linux.
I like linux.
I like playing computer game.
I like playing computer game.
My qq is 747356101.
My wechat is cpu19900130.
My website is https://chupeng.site.
#2.只输出2和3行,p在引号内外皆可!
root@baiduyun:/tmp# sed -n '2,3p' cp.txt
I like linux.
I like playing computer game.
#3.只输出2和3行
root@baiduyun:/tmp# sed -n '2,3'p cp.txt
I like linux.
I like playing computer game.

3. 输出第2行及其往后3行的内容

sed -n "2,+3p" cp.txt
root@baiduyun:/tmp# sed -n "2,+3p" cp.txt
I like linux.
I like playing computer game.
My qq is 747356101.
My wechat is cpu19900130.

4. 输出含有linux的行,类似 grep !

sed -n "/linux/p" cp.txt
root@baiduyun:/tmp# sed -n "/linux/p" cp.txt
I like linux.

5. 删除含有game的行(内存中修改,不修改原文件)

sed -n "/game/d" cp.txt

# 查看删除后的结果
sed "/game/d" cp.txt
# 删除默认不输出内容
root@baiduyun:/tmp# sed -n "/game/d" cp.txt
root@baiduyun:/tmp#
# 查看删除后的结果,此结果是在内存中,原文件未修改!
root@baiduyun:/tmp# sed "/game/d" cp.txt
My name is chupeng.
I like linux.
My qq is 747356101.
My wechat is cpu19900130.
My website is https://chupeng.site.

6. 删除含有game的行(直接修改原文件)

sed -i "/game/d" cp.txt

cat cp.txt
root@baiduyun:/tmp# cat cp.txt
My name is chupeng.
I like linux.
I like playing computer game.  # 执行后,原文件此行消失
My qq is 747356101.
My wechat is cpu19900130.
My website is https://chupeng.site.
root@baiduyun:/tmp#
root@baiduyun:/tmp# sed -i "/game/d" cp.txt
root@baiduyun:/tmp#
root@baiduyun:/tmp# cat cp.txt
My name is chupeng.
I like linux.
My qq is 747356101.
My wechat is cpu19900130.
My website is https://chupeng.site.

7. 删除第4行到结尾的内容

sed '4,$d' cp.txt
root@baiduyun:/tmp# sed '4,$d' cp.txt
My name is chupeng.
I like linux.
My qq is 747356101.

8. 将My替换为His(常用于快速修改配置文件!加上参数-i直接修改文件!)

sed 's/My/His/g' cp.txt
root@baiduyun:/tmp# sed 's/My/His/g' cp.txt
His name is chupeng.
I like linux.
His qq is 747356101.
His wechat is cpu19900130.
His website is https://chupeng.site.

9. 替换His->My,同时替换掉QQ号为88888

sed -e 's/His/My/g' -e 's/747356101/88888/g' cp.txt
root@baiduyun:/tmp# sed -e 's/His/My/g' -e 's/747356101/88888/g' cp.txt
My name is chupeng.
I like linux.
My qq is 88888.
My wechat is cpu19900130.
My website is https://chupeng.site.

10. 在第2行后追加内容,并写入到文件

sed -i '2a I am using sed command.' cp.txt 
root@baiduyun:/tmp# sed '2a I am using sed command.' cp.txt
My name is chupeng.
I like linux.
I am using sed command.
My qq is 747356101.
My wechat is cpu19900130.
My website is https://chupeng.site.
root@baiduyun:/tmp# sed -i '2a I am using sed command.' cp.txt
root@baiduyun:/tmp#
root@baiduyun:/tmp# cat cp.txt
My name is chupeng.
I like linux.
I am using sed command.
My qq is 747356101.
My wechat is cpu19900130.
My website is https://chupeng.site.

11. 在第4行前插入2行内容

sed -i '4i My linux is good.\nI like pretty girl.' cp.txt
root@baiduyun:/tmp# sed -i '4i My linux is good.\nI like pretty girl.' cp.txt
root@baiduyun:/tmp#
root@baiduyun:/tmp# cat cp.txt
My name is chupeng.
I like linux.
I am using sed command.
My linux is good.
I like pretty girl.
My linux is good.
I like pretty girl.
My qq is 747356101.
My wechat is cpu19900130.
My website is https://chupeng.site.

12. 在每一行下,添加一行内容

sed "a -------------------" cp.txt
root@baiduyun:/tmp# sed "a -------------------" cp.txt
My name is chupeng.
-------------------
I like linux.
-------------------
I am using sed command.
-------------------
My linux is good.
-------------------
I like pretty girl.
-------------------
My linux is good.
-------------------
I like pretty girl.
-------------------
My qq is 747356101.
-------------------
My wechat is cpu19900130.
-------------------
My website is https://chupeng.site.
-------------------

5. sed配合正则表达式企业案例

1. 取出Linux的IP地址

# 思路:
#1.先取出第2行
ifconfig |sed -n '2p'

#2. 找到第2行后,去掉ip之前的内容
ifconfig |sed -n '2p' | sed 's/^.*inet//'

#3. 去掉ip之后的内容,即可得到IP!
ifconfig |sed -n '2p' | sed 's/^.*inet//' |sed 's/net.*$//'

#4.也可以使用-e进行多次编辑
ifconfig|sed -n -e '2s/^.*inet//g' -e '2s/net.*$//g'p
root@baiduyun:/tmp# ifconfig |sed -n '2p'
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
root@baiduyun:/tmp# ifconfig |sed -n '2p' | sed 's/^.*inet//'
 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
root@baiduyun:/tmp#
root@baiduyun:/tmp# ifconfig |sed -n '2p' | sed 's/^.*inet//' |sed 's/net.*$//'
 172.18.0.1

root@baiduyun:/tmp# ifconfig|sed -n -e '2s/^.*inet//g' -e '2s/net.*$//g'p
 172.18.0.1

6. 取出nginx访问日志中指定时间段的日志!

cd /var/log/nginx
cat access.log.1
# 16/Jan/2022:09:14:47
# 16/Jan/2022:09:19:49 

sed -n '/16\/Jan\/2022:09:14:47/,/16\/Jan\/2022:09:19:49/'p access.log.1
# 使用错误日志代替
root@baiduyun:/var/log/nginx# cat error.log.1
2022/02/09 21:20:10 [notice] 3563674#3563674: signal process started
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:10 [emerg] 3561765#3561765: still could not bind()
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: still could not bind()
2022/02/09 21:20:41 [notice] 3564633#3564633: signal process started
# 取出20:17-20:41时间段内的内容
# 2022/02/09 21:20:17
# 2022/02/09 21:20:41
# sed -n '/2022\/02\/09 21:20:17/,/2022\/02\/09 21:20:41/'p error.log.1
root@baiduyun:/var/log/nginx# sed -n '/2022\/02\/09 21:20:17/,/2022\/02\/09 21:20:41/'p error.log.1
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: bind() to [::]:8080 failed (98: Address already in use)
2022/02/09 21:20:17 [emerg] 3563955#3563955: still could not bind()
2022/02/09 21:20:41 [notice] 3564633#3564633: signal process started


最后更新: 2022-02-15 08:59:03