sed命令详解

主要参考:
(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/原字符串/替换字符串/'

注意:

  1. 若存在特殊字符,需要使用反斜线\进行转义;
  2. 若存在单引号,外层的单引号改成双引号 sed "s/原字符串/替换字符串/"
  3. 删除某一个字符串可写为: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)插入空行

  1. 每行后面插入一行空行:sed G test.txt
  2. 每行后面插入两行空行:sed 'G;G' test.txt
  3. 每行前面插入一行空行:sed 'x;p;x' test.txt
  4. 每行前面插入两行空行: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) 在匹配位置的前后添加空行

  1. 如果一行里面有big这个单词,那么在他后面插入一个空行:sed '/big/G' test.txt
  2. 如果一行里面有big这个单词,那么在他前后各插入一个空行:sed '/big/{x;p;x;G}' test.txt
  3. 在第二行前后,插入空行:sed '2{x;p;x;G}' test.txt
  4. 以a开头的行后面添加行:sed '/^a/G' test.txt
    在这里插入图片描述

(5)每隔几行插入空行

  1. 每隔1行后面增加一个空行:sed 'N;G' test.txt
  2. 每隔两行后面增加一个空行: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的功能还有很多,等慢慢添加进来

  • 20
    点赞
  • 310
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺旺棒棒冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值