typora/note/Shell/sed.md
2024-12-12 10:48:55 +08:00

212 lines
4.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### 概念
- stream editor的缩写
- 流编辑器,用来处理文件
- 一行一行读取文件,按照要求进行处理,并把结果输出到屏幕
- 对缓冲区中副本进行编辑,不会修改源文件
### 运行模式
- 命令行模式
- 脚本模式
### 命令行格式
- sed options ‘处理动作’ 文件名
![294a6fb86ea2c755baf11d4517227967.png](../../_resources/294a6fb86ea2c755baf11d4517227967.png)
- 常见处理动作
![21f9477ffd1ea12c46e439205e44a117.png](../../_resources/21f9477ffd1ea12c46e439205e44a117.png)
### 举例
- 打印文件 `sed '' passwd`
- 打印文件第2行
```
me@me-EQ59:~/shell_demo$ sed -n '2p' passwd
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
me@me-EQ59:~/shell_demo$
```
- 打印文件 1-4 行
```
me@me-EQ59:~/shell_demo$ sed -n '1,4p' passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
me@me-EQ59:~/shell_demo$
```
- 打印文件文件最后一行
```
me@me-EQ59:~/shell_demo$ sed -n '$p' passwd
gitlab-runner:x:1001:1001:GitLab Runner:/home/gitlab-runner:/bin/bash
me@me-EQ59:~/shell_demo$
```
- 文件第二行前插入内容
```
me@me-EQ59:~/shell_demo$ sed -n '2ihello world' passwd
hello world
```
- 文件第三行后插入内容
```
sed -n '3ahello world' paswd
```
- 支持换行插入内容
```
me@me-EQ59:~/shell_demo$ sed '3ihello\
world\
999\
flal' file1 # 命令到这结束后 enter
lfla
fla
hello
world
999
flal
flavnanv
rpprp
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$
```
- 多行之间插入,在第二行和第三行和第四行前边插入内容
```
me@me-EQ59:~/shell_demo $ sed '2,4ihello' file1
lfla
hello
fla
hello
flavnanv
hello
rpprp
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$
```
- 替换整行内容,替换第五行内容
```
me@me-EQ59:~/shell_demo$ cat file1
lfla
fla
flavnanv
rpprp
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$ sed '5cheloworld' file1
lfla
fla
flavnanv
rpprp
heloworld
me@me-EQ59:~/shell_demo$
```
- 替换文件所有内容 sed 'hello world' file1
- 替换多行内容,替换 1-5 行的内容为 hello world
```
me@me-EQ59:~/shell_demo$ cat file1
lfla
fla
flavnanv
rpprp
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$ sed '2,4cheloworld' file1
lfla
heloworld
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$
```
- fla 开头的行替换成 haha
```
me@me-EQ59:~/shell_demo$ cat file1
lfla
fla
flavnanv
rpprp
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$ sed '/^fla/chaha' file1
lfla
haha
haha
rpprp
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$
```
- 删除第四行 `sed '4d' file1`
- 删除 1 到 4 行 `sed '1,4d' file1`
### 其他命令
![2d661ab455541517d45e666109787aab.png](../../_resources/2d661ab455541517d45e666109787aab.png)
- 读取其他文件的内容到指定位置,读取/etc/hosts 文件的内容,放到 第一行下边
```
me@me-EQ59:~/shell_demo$ sed '1r /etc/hosts' file1
lfla
127.0.0.1 localhost
127.0.1.1 me-EQ59
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
fla
flavnanv
rpprp
/usr/local
/user/local/bin
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$
```
- 保存当前文件的多行到当前目下某一个文件内
```
me@me-EQ59:~/shell_demo$ cat file1
lfla
fla
flavnanv
rpprp
/usr/local
/user/local/bin
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$ sed -n '2,4w another.txt' file1
me@me-EQ59:~/shell_demo$ ls
another.txt EOF file1 hello_grep.txt ip.txt passwd passwd.bak scripts test1
me@me-EQ59:~/shell_demo$ cat another.txt
fla
flavnanv
rpprp
me@me-EQ59:~/shell_demo$
```
- 保留匹配串fla开头的行前边加注释
```
me@me-EQ59:~/shell_demo$ cat file
lfla
fla
flavnanv
rpprp
/usr/local
/user/local/bin
dbdfblbjaljl;ka
me@me-EQ59:~/shell_demo$ sed -n 's/^fla/#&/gp' file
#fla
#flavnanv
me@me-EQ59:~/shell_demo$
```
- 多次编辑,打印包含 root 的行,以及行号
```
me@me-EQ59:~/shell_demo$ sed -ne '/root/p;/root/=' passwd
root:x:0:0:root:/root:/bin/bash
1
nm-openvpn:x:121:127:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin
40
me@me-EQ59:~/shell_demo$
```