+ -
当前位置:首页 → 问答吧 → sed怎么把多条命令放在同一行呢?

sed怎么把多条命令放在同一行呢?

时间:2011-05-05

来源:互联网

如果想把多个sed命令放到同一行,那么可以使用分号(;)来分割这些命令~能举个例子吗?
  1. daya@daya-desktop:~/sed$ cat new
  2. Line one.
  3. The second line.
  4. The third.
  5. This is line four.
  6. Five.
  7. This is sixth sentence.
  8. This is line seven.
  9. Eighth and last.
复制代码
  1. daya@daya-desktop:~/sed$ sed '2 a\123
  2. > 4 a\456' new
  3. Line one.
  4. The second line.
  5. 123
  6. The third.
  7. This is line four.
  8. 456
  9. Five.
  10. This is sixth sentence.
  11. This is line seven.
  12. Eighth and last.
复制代码
我在第二行和第四行后面分别添加123,456,现在该如何把它们写在同一行呢?谢谢~
  1. daya@daya-desktop:~/sed$ sed '2 a\123 ; a \456' new
  2. Line one.
  3. The second line.
  4. 123 ; a 456
  5. The third.
  6. This is line four.
  7. Five.
  8. This is sixth sentence.
  9. This is line seven.
  10. Eighth and last.
复制代码

作者: ljsdaya   发布时间: 2011-05-05

a命令确实要换行

作者: cjaizss   发布时间: 2011-05-05

除非你修改一下你的程序
sed '2s/$/\n123/;4s/$/\n456/'

作者: cjaizss   发布时间: 2011-05-05

这个?

sed -e '2a 123' -e '4a 456'

作者: yinyuemi   发布时间: 2011-05-05

回复 cjaizss


    嗯,那格式就是: sed 'command;command ...' file-list

作者: ljsdaya   发布时间: 2011-05-05

回复 yinyuemi


    这样也可以哦 每次 -e 后面加一个命令 ,,,

作者: ljsdaya   发布时间: 2011-05-05



QUOTE:
回复  yinyuemi


    这样也可以哦 每次 -e 后面加一个命令 ,,,
ljsdaya 发表于 2011-05-05 12:47




    恩,也对,-e就相当于换行了

作者: cjaizss   发布时间: 2011-05-05