主要参考:
(1)使用sed输出文件的指定行
(2)sed 字符串替换
(3)sed之添加空行
仅供自己学习使用,如有侵权,请联系删除
获取或删除指定的行
(1) 获取test.txt的第二行,输入到屏幕
sed -n 2p test.txt
删除test.txt的第二行
sed -i 2d test.txt
(2) 获取test.txt的第一行至第二行
sed -n 1,2p test.txt
# 加不加引号均可 sed -n ‘1,2p’ test.txt
删除test.txt的第一行至第二行
sed -i 1,2d test.txt
(3)获取test.txt的第二行至最后一行
sed -n '2,$p' test.txt
# 必须加单引号
删除test.txt的第二行至最后一行
sed -i '2,$d' test.txt
# 必须加单引号
(4)匹配test.txt中以c开头的行输出或者不输出
sed -n '/^c/p' test.txt
# 输出, 可以是任何其他的正则表达式
sed -n '/^c/!p' test.txt
# 不输出
字符串替换
(1)只替换匹配到的第一个
sed 's/原字符串/替换字符串/'
注意:
- 若存在特殊字符,需要使用反斜线
\
进行转义; - 若存在单引号,外层的单引号改成双引号
sed "s/原字符串/替换字符串/"
- 删除某一个字符串可写为:
sed 's/原字符串//g'
(2) 替换所有匹配到的关键字
sed 's/原字符串/替换字符串/g'
# 在末尾加g替换每一个匹配的关键字,否则只替换每行的第一个
sed处理过的输出是直接输出到屏幕上的,使用参数-i
直接在文件中替换: sed -i 's/原字符串/替换字符串/g'
(3)指定某一行进行替换,其他行不处理
sed '2s/原字符串/替换字符串/g' #替换第2行
sed '$s/原字符串/替换字符串/g' #替换最后一行
sed '2,5s/原字符串/替换字符串/g' #替换2到5行
sed '2,$s/原字符串/替换字符串/g' #替换2到最后一行
(4)进行多个替换,用分号隔开
sed 's/原字符串/替换字符串/g; s/原字符串/替换字符串/g'
注意:命令执行是有先后顺序的。
如上图第二个命令,先将文本中的a替换成b,再将b替换成c。原来是a的位置也会被替换成c
(5)在特定的位置,添加内容
注意:这里的&符号。
如果没有&, 就会直接将匹配到的字符串替换掉
sed 's/^/添加的头部&/g' test.txt #在所有行首添加
sed 's/$/&添加的尾部/g' test.txt #在所有行末添加
sed 's/big/xx&/g' test.txt #在所有行字符串big前面添加XX
sed 's/big/&xx/g' test.txt #在所有行字符串big后面添加XX
空行处理
(1)删除空行
sed '/^$/d' test.txt
(2)插入空行
- 每行后面插入一行空行:
sed G test.txt
- 每行后面插入两行空行:
sed 'G;G' test.txt
- 每行前面插入一行空行:
sed 'x;p;x' test.txt
- 每行前面插入两行空行:
sed 'x;p;x;x;p;x' test.txt
依次类推,添加几行空行,就有几个G
或者x;p;x
(3) 先删除行后有空行,再在每行后面添加空行: sed '/^$/d;G' test.txt
sed '/^$/d;G' test.txt
多个操作用分号隔开
(4) 在匹配位置的前后添加空行
- 如果一行里面有
big
这个单词,那么在他后面插入一个空行:sed '/big/G' test.txt
- 如果一行里面有
big
这个单词,那么在他前后各插入一个空行:sed '/big/{x;p;x;G}' test.txt
- 在第二行前后,插入空行:
sed '2{x;p;x;G}' test.txt
- 以a开头的行后面添加行:
sed '/^a/G' test.txt
(5)每隔几行插入空行
- 每隔1行后面增加一个空行:
sed 'N;G' test.txt
- 每隔两行后面增加一个空行:
sed 'N;N;G' test.txt
以此类推,每个几行就有几个N;
(base) root@4283fca5e17d:~# sed
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-E, -r, --regexp-extended
use extended regular expressions in the script
(for portability use POSIX -E).
-s, --separate
consider files as separate rather than as a single,
continuous long stream.
--sandbox
operate in sandbox mode.
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
-z, --null-data
separate lines by NUL characters
--help display this help and exit
--version output version information and exit
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
sed的功能还有很多,等慢慢添加进来