From 8b3e8f621bad587099cf1c210ac4ac30fdbdcd85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=BC=9F=E8=B0=83=E8=B0=83?= Date: Thu, 25 May 2023 09:35:07 +0800 Subject: [PATCH] doc: Update sed.md (#503) --- command/sed.md | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/command/sed.md b/command/sed.md index 949cde4b18..108fa1c44f 100644 --- a/command/sed.md +++ b/command/sed.md @@ -223,6 +223,7 @@ ifconfig ens32 | sed -n '/inet /p' | sed 's/inet \([0-9.]\+\).*/\1/' ``` ### 大小写转换U/L + ```shell \u: 首字母转换为大写 \U: 全部转换为大写 @@ -250,14 +251,26 @@ BIN:x:1:1:bin:/bin:/sbin/nologin ### 组合多个表达式 +1. 替换文本中的多个字符串: + ```shell -sed '表达式' | sed '表达式' - -等价于: - -sed '表达式; 表达式' +sed -e 's/old_string/new_string/g' -e 's/another_old_string/another_new_string/g' file.txt ``` +2. 删除文本中的多个行: + +```shell +sed -e '1d' -e '/pattern/d' file.txt +``` + +3. 在文本中插入多个行: + +```shell +sed -e '1i\inserted_line1' -e '2i\inserted_line2' file.txt +``` + +其中,-e 表示指定一个表达式,多个表达式之间用 -e 分隔。每个表达式可以是一个 sed 命令,例如 s、d、i 等。 + ### 引用 sed表达式可以使用单引号来引用,但是如果表达式内部包含变量字符串,就需要使用双引号。