0


Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)

0 前言

Linux shell编程学习笔记73:sed命令——沧海横流任我行(上)-CSDN博客文章浏览阅读684次,点赞32次,收藏24次。在大数据时代,我们要面对大量数据,有时需要对数据进行替换、删除、新增、选取等特定工作。在Linux中提供很多数据处理命令,如果我们要以行为单位进行数据处理,可以使用sed命令。https://blog.csdn.net/Purpleendurer/article/details/141307421?spm=1001.2014.3001.5501中,我们研究了sed的基础知识。

Linux shell编程学习笔记74:sed命令——沧海横流任我行(中)-CSDN博客https://blog.csdn.net/Purpleendurer/article/details/141369789?spm=1001.2014.3001.5501中,我们见识了sed删除和替换功能的威力。

现在我们通过一些实例来见识一下sed插入等功能的威力。

1 sed实列

1.1 插入行

1.1.1 前插

我们可以使用i命令(insert)来完成前插。

1.1.1.1 在第3行、第4行前插入'abc'

命令为:

sed '3,4i\abc'

其中sed命令的参数说明如下:

3,4 :指定第3行、第4行

i:在前插入命令

\abc:要插入的字符串

  1. [purpleendurer @ bash ~] seq 7 | cat -n
  2. 1 1
  3. 2 2
  4. 3 3
  5. 4 4
  6. 5 5
  7. 6 6
  8. 7 7
  9. [purpleendurer @ bash ~] seq 7 | cat -n | sed '3,4i\abc'
  10. 1 1
  11. 2 2
  12. abc
  13. 3 3
  14. abc
  15. 4 4
  16. 5 5
  17. 6 6
  18. 7 7
  19. [purpleendurer @ bash ~]

1.1.1.2 在开头前插入'abc'

命令为:

sed -e '1i\abc'

其中sed命令的参数说明如下:

-e:解释脚本

1 :指定第1行

i:在前插入命令

\abc:要插入的字符串

  1. [purpleendurer @ bash ~] seq 7 | cat -n
  2. 1 1
  3. 2 2
  4. 3 3
  5. 4 4
  6. 5 5
  7. 6 6
  8. 7 7
  9. [purpleendurer @ bash ~] seq 7 | cat -n | sed -e '1i\abc'
  10. abc
  11. 1 1
  12. 2 2
  13. 3 3
  14. 4 4
  15. 5 5
  16. 6 6
  17. 7 7
  18. [purpleendurer @ bash ~]

1.1.1.3 在所有包含1的行前插入'abc'

命令为:

** sed '/1*/i\abc'**

  1. [purpleendurer @ bash ~] seq 11 | cat -n
  2. 1 1
  3. 2 2
  4. 3 3
  5. 4 4
  6. 5 5
  7. 6 6
  8. 7 7
  9. 8 8
  10. 9 9
  11. 10 10
  12. 11 11
  13. [purpleendurer @ bash ~] seq 11 | cat -n | sed '/1*/i\abc'
  14. abc
  15. 1 1
  16. abc
  17. 2 2
  18. abc
  19. 3 3
  20. abc
  21. 4 4
  22. abc
  23. 5 5
  24. abc
  25. 6 6
  26. abc
  27. 7 7
  28. abc
  29. 8 8
  30. abc
  31. 9 9
  32. abc
  33. 10 10
  34. abc
  35. 11 11
  36. [purpleendurer @ bash ~]

1.1.2 追加(后插)

我们可以使用a命令(append)来完成追加(后插)。

1.1.2.1在第3行及其后2行后追加'abc'

命令为:

sed '3,+2a\abc'

  1. [purpleendurer @ bash ~] seq 7 | cat -n
  2. 1 1
  3. 2 2
  4. 3 3
  5. 4 4
  6. 5 5
  7. 6 6
  8. 7 7
  9. [purpleendurer @ bash ~] seq 7 | cat -n | sed '3,+2a\abc'
  10. 1 1
  11. 2 2
  12. 3 3
  13. abc
  14. 4 4
  15. abc
  16. 5 5
  17. abc
  18. 6 6
  19. 7 7
  20. [purpleendurer @ bash ~]

1.1.2.2 在末尾追加'abc'

命令为:

sed '$a\abc'

  1. [purpleendurer @ bash ~] seq 7 | cat -n
  2. 1 1
  3. 2 2
  4. 3 3
  5. 4 4
  6. 5 5
  7. 6 6
  8. 7 7
  9. [purpleendurer @ bash ~] seq 7 | cat -n | sed '$a\abc'
  10. 1 1
  11. 2 2
  12. 3 3
  13. 4 4
  14. 5 5
  15. 6 6
  16. 7 7
  17. abc
  18. [purpleendurer @ bash ~]

1.1.2.3 在偶数行后追加2行信息,第1行是abc,第2行是def

命令为:

sed '0~2a\abc
def'

  1. [purpleendurer @ bash ~] seq 5 | cat -n
  2. 1 1
  3. 2 2
  4. 3 3
  5. 4 4
  6. 5 5
  7. [purpleendurer @ bash ~] seq 5 | cat -n | sed '0~2a\abc\
  8. def'
  9. 1 1
  10. 2 2
  11. abc
  12. def
  13. 3 3
  14. 4 4
  15. abc
  16. def
  17. 5 5
  18. [purpleendurer @ bash ~]

1.1.2.4 在所有包含1的行后面追加abc

命令为:

** sed '/1/aabc'**

  1. [purpleendurer @ bash ~] seq 10 | cat -n
  2. 1 1
  3. 2 2
  4. 3 3
  5. 4 4
  6. 5 5
  7. 6 6
  8. 7 7
  9. 8 8
  10. 9 9
  11. 10 10
  12. [purpleendurer @ bash ~] seq 10 | cat -n | sed '/1/aabc'
  13. 1 1
  14. abc
  15. 2 2
  16. 3 3
  17. 4 4
  18. 5 5
  19. 6 6
  20. 7 7
  21. 8 8
  22. 9 9
  23. 10 10
  24. abc
  25. [purpleendurer @ bash ~]

1.1.2.5 在只含有1个1的行后面追加abc

命令为:

** sed '/1$/aabc'**

  1. [purpleendurer @ bash ~] seq 10 | cat -n
  2. 1 1
  3. 2 2
  4. 3 3
  5. 4 4
  6. 5 5
  7. 6 6
  8. 7 7
  9. 8 8
  10. 9 9
  11. 10 10
  12. [purpleendurer @ bash ~] seq 10 | cat -n | sed '/1$/aabc'
  13. 1 1
  14. abc
  15. 2 2
  16. 3 3
  17. 4 4
  18. 5 5
  19. 6 6
  20. 7 7
  21. 8 8
  22. 9 9
  23. 10 10
  24. [purpleendurer @ bash ~]

1.2 打印行号

我们可以使用等号(=)来代表行号。

命令为:

** sed '='**

  1. [purpleendurer @ bash ~] echo "aaa" > t.txt
  2. [purpleendurer @ bash ~] echo "bbb" >> t.txt
  3. [purpleendurer @ bash ~] echo "ccc" >> t.txt
  4. [purpleendurer @ bash ~] cat t.txt
  5. aaa
  6. bbb
  7. ccc
  8. [purpleendurer @ bash ~] sed '=' t.txt
  9. 1
  10. aaa
  11. 2
  12. bbb
  13. 3
  14. ccc
  15. [purpleendurer @ bash ~]


本文转载自: https://blog.csdn.net/Purpleendurer/article/details/141439050
版权归原作者 紫郢剑侠 所有, 如有侵权,请联系我们删除。

“Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)”的评论:

还没有评论