Create New Document

The title of your document (will be displayed as H1)
URL-friendly name (no spaces, use dashes)
Path where to create document (optional, use forward slashes to create subdirectories)

Move/Rename Document

Current location of the document
New path for the document (including the slug)
This only changes the document's path. It does not modify the document's title (H1 heading).

Delete Document

Are you sure you want to delete this document? This action cannot be undone.

Warning: If this is a folder, all contents including subfolders and documents will be deleted.

Message

Message content goes here.

Confirm Action

Are you sure?

Attachments

Allowed file types: jpg, jpeg, png, gif, svg, webp, txt, log, csv, sfd, zip, pdf, docx, xlsx, pptx, mp4 (Max: 10MB)

Document Files

Loading attached files...

Document History

Previous Versions

Loading versions...

Preview

Select a version to preview

Wiki Settings

Language for the user interface
Number of versions to keep per document. Set to 0 to disable versioning.
Maximum allowed file size for uploads in MB.

User Management

Add New User

Leave empty to keep current password
Users with these groups can access restricted sections.

Define path-based access rules for sections of your wiki, then assign users to groups in the Users tab. Rules are evaluated in order. First match wins.

Active Rules

Import markdown files from a ZIP archive. Files will be processed and stored in the appropriate document structure. Directory structure in the ZIP (category/subcategory) will be preserved in the wiki.

Upload a ZIP file containing markdown (.md) files to import.

Create and manage backups of your wiki data. Backups include all documents, images, and configuration files.

Available Backups

Loading backups...

Add/Edit Access Rule

Selected: /

Add Column

Banner

sed

sed 命令完全指南

sed(Stream Editor)是 Linux 中的流编辑器,用于对文本进行过滤和转换。它非常适合在脚本中批量处理文本。


一、命令语法

sed [选项] '脚本' [文件]
sed -i '脚本' 文件        # 直接修改文件

基本工作方式

  1. 逐行读取文件
  2. 对每一行执行指定的编辑命令
  3. 输出结果(默认不修改原文件)

二、常用选项

选项 说明
-i 直接修改文件(原地编辑)
-i.bak 修改前备份为 .bak 文件
-n 不自动输出(配合 p 使用)
-e 执行多个脚本
-r / -E 使用扩展正则表达式

三、核心命令

命令 说明 示例
s/旧/新/ 替换 s/old/new/
s/旧/新/g 全局替换(所有匹配) s/old/new/g
s/旧/新/2 替换每行第2个匹配 s/old/new/2
d 删除行 /pattern/d
p 打印行(配合 -n) /pattern/p
a\ 在行后追加 a\new line
i\ 在行前插入 i\new line
c\ 替换行 c\new line
= 显示行号 =
q 退出 10q

四、常用案例

1. 替换操作(最常用)

替换第一个匹配

sed 's/old/new/' file.txt
# 每行第一个 old 替换为 new

# 示例:替换第一个 hello
echo "hello hello world" | sed 's/hello/hi/'
# 输出:hi hello world

全局替换(所有匹配)

sed 's/old/new/g' file.txt
# 所有 old 替换为 new

# 示例:替换所有 hello
echo "hello hello world" | sed 's/hello/hi/g'
# 输出:hi hi world

大小写不敏感替换

sed 's/old/new/gi' file.txt
# i 表示忽略大小写

替换第 N 个匹配

echo "a a a a" | sed 's/a/b/2'
# 输出:a b a a(只替换第二个)

使用扩展正则

sed -r 's/[0-9]+/NUM/g' file.txt
# 将所有数字替换为 NUM

2. 删除操作

删除指定行

# 删除第3行
sed '3d' file.txt

# 删除第2-5行
sed '2,5d' file.txt

# 删除最后一行
sed '$d' file.txt

# 删除空行
sed '/^$/d' file.txt

# 删除包含 pattern 的行
sed '/pattern/d' file.txt

# 删除不包含 pattern 的行
sed '/pattern/!d' file.txt

删除注释行

# 删除以 # 开头的行
sed '/^#/d' file.txt

# 删除所有注释行(包括行首有空格)
sed '/^[[:space:]]*#/d' file.txt

3. 插入和追加

在行前插入

# 在第2行前插入
sed '2i\这是插入的行' file.txt

# 在匹配行前插入
sed '/pattern/i\插入内容' file.txt

在行后追加

# 在第2行后追加
sed '2a\这是追加的行' file.txt

# 在匹配行后追加
sed '/pattern/a\追加内容' file.txt

替换整行

# 将第2行替换为新内容
sed '2c\新内容' file.txt

# 将匹配的行替换
sed '/pattern/c\新内容' file.txt

4. 打印和查看

打印匹配行

# 打印包含 pattern 的行
sed -n '/pattern/p' file.txt

# 打印第2行
sed -n '2p' file.txt

# 打印第2-5行
sed -n '2,5p' file.txt

# 打印匹配行及其后3行
sed -n '/pattern/,+3p' file.txt

打印行号

sed -n '/pattern/=' file.txt
# 显示匹配行的行号

5. 直接修改文件(-i 选项)

# 直接修改文件(危险操作,先备份)
sed -i 's/old/new/g' file.txt

# 修改前备份(推荐)
sed -i.bak 's/old/new/g' file.txt
# 生成 file.txt.bak 备份文件

# 多个修改
sed -i 's/old/new/g; s/foo/bar/g' file.txt

五、常用组合案例

1. 批量替换文件内容

# 替换所有 .txt 文件中的 foo 为 bar
sed -i 's/foo/bar/g' *.txt

# 递归替换当前目录所有文件
find . -type f -exec sed -i 's/old/new/g' {} \;

# 只替换配置文件
find . -name "*.conf" -exec sed -i 's/old/new/g' {} \;

2. 删除空行和注释

# 删除空行和以 # 开头的行
sed -e '/^$/d' -e '/^#/d' file.txt

# 更简洁的方式
sed '/^#/d; /^$/d' file.txt

3. 提取特定内容

# 提取 IP 地址(假设每行一个 IP)
sed -n 's/.*\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p' file.txt

# 提取错误日志行
sed -n '/ERROR/p' app.log

4. 在文件开头/结尾添加内容

# 在文件开头添加一行
sed '1i\第一行内容' file.txt

# 在文件结尾添加一行
sed '$a\最后一行内容' file.txt

# 在文件开头添加多行
sed '1i\第一行\n第二行' file.txt

5. 修改配置文件

Nginx 配置修改

# 修改端口号
sed -i 's/listen 80;/listen 8080;/' nginx.conf

# 修改 server_name
sed -i 's/server_name localhost;/server_name example.com;/' nginx.conf

修改网络配置

# 修改 IP 地址
sed -i 's/192.168.1.100/192.168.1.200/' /etc/network/interfaces

6. 批量重命名变量

# 在代码中批量修改变量名
sed -i 's/old_variable/new_variable/g' *.py

7. 添加或删除行

在匹配行后面添加

# 在包含 ServerName 的行后添加一行
sed -i '/ServerName/a\    ServerAlias www.example.com' apache.conf

在匹配行前面插入

# 在包含 include 的行前插入
sed -i '/include/i\# new configuration' config.conf

删除特定行

# 删除包含 "debug" 的行
sed -i '/debug/d' log.txt

# 删除第3行到结尾
sed -i '3,$d' file.txt

8. 多行操作

# 合并多行(将换行符替换为空格)
sed ':a;N;$!ba;s/\n/ /g' file.txt

# 删除空白行
sed '/^$/d' file.txt

# 添加空行
sed 'G' file.txt        # 每行后加空行
sed '/^$/d;G' file.txt  # 压缩多行空行为一行

9. 复杂替换

# 使用 & 引用匹配的完整字符串
echo "hello world" | sed 's/hello/【&】/'
# 输出:【hello】 world

# 使用 \1, \2 引用分组(需要 -r 或 -E)
sed -r 's/(\w+) (\w+)/\2 \1/' file.txt
# 交换两个词

# 在每行末尾添加内容
sed 's/$/ ---END---/' file.txt

# 在每行开头添加行号
sed = file.txt | sed 'N;s/\n/ /'

10. 多个编辑组合

# 使用 -e 执行多个操作
sed -e 's/foo/bar/g' -e '/^$/d' -e 's/^/前缀 /' file.txt

# 使用分号分隔
sed 's/foo/bar/g; /^$/d; s/^/前缀 /' file.txt

# 使用脚本文件
cat > sed_script.sed << 'EOF'
s/foo/bar/g
/^$/d
s/^/前缀 /
EOF

sed -f sed_script.sed file.txt

六、常用模式速查

模式 含义
^ 行首
$ 行尾
. 任意单个字符
* 0次或多次
\+ 1次或多次(需 -r)
\? 0次或1次(需 -r)
[0-9] 数字
[a-zA-Z] 字母
\s 空白字符
\S 非空白字符
\w 单词字符
` `
\( \) 分组
\{n\} n 次

七、快速参考表

需求 命令
替换第一个匹配 sed 's/old/new/' file
替换所有匹配 sed 's/old/new/g' file
直接修改文件 sed -i 's/old/new/g' file
删除空行 sed '/^$/d' file
删除注释行 sed '/^#/d' file
删除匹配行 sed '/pattern/d' file
打印匹配行 sed -n '/pattern/p' file
行后追加 sed '/pattern/a\内容' file
行前插入 sed '/pattern/i\内容' file
替换整行 sed '/pattern/c\内容' file
显示行号 sed '/pattern/=' file
多个操作 sed -e 'cmd1' -e 'cmd2' file

八、注意事项

  1. -i 会直接修改文件,建议先备份
  2. 正则表达式-r 模式下更简单
  3. 默认只替换每行第一个匹配,需要 /g 才全局替换
  4. 使用单引号避免 shell 解释特殊字符
  5. 复杂操作可以先不用 -i,测试正确后再使用

安全操作示例

# 1. 先查看效果(不修改)
sed 's/old/new/g' file.txt

# 2. 确认无误后修改(自动备份)
sed -i.bak 's/old/new/g' file.txt

# 3. 检查结果
diff file.txt.bak file.txt

Attached Files

Loading attached files...
Search Results