• Linux常用基本命令:三剑客命令之-sed


    sed是一个很强大的文件处理工具,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作

    格式:sed [option] [command] [file]

    常用命令:

            a   ∶新增
            c   ∶取代
            d   ∶删除
             i   ∶插入
             p  ∶列印
             s  ∶取代

    选项:

         -i∶直接修改读取的档案内容,而不是由萤幕输出。       

        -n∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。

    1,sed '1d' ghostwu.com   d代表删除 d前面的数字代表删除第一行,该命令不会修改文件本身

    ghostwu@dev:~/linux/sed$ cat -n ghostwu.txt 
         1    this is ghostwu
         2    how are you
         3    hod old are you
         4    and you
         5    fine thank you
         6    come with me!!!
    ghostwu@dev:~/linux/sed$ sed '1d' ghostwu.txt 
    how are you
    hod old are you
    and you
    fine thank you
    come with me!!!
    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    hod old are you
    and you
    fine thank you
    come with me!!!

    2,删除最后一行,$代表最后一行

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    hod old are you
    and you
    fine thank you
    come with me!!!
    ghostwu@dev:~/linux/sed$ sed '$d' ghostwu.txt 
    this is ghostwu
    how are you
    hod old are you
    and you
    fine thank you

    3,删除第一行到第二行

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    hod old are you
    and you
    fine thank you
    come with me!!!
    ghostwu@dev:~/linux/sed$ sed '1,2d' ghostwu.txt 
    hod old are you
    and you
    fine thank you
    come with me!!!

    4,删除第二行到最后一行

    ghostwu@dev:~/linux/sed$ sed '2,$d' ghostwu.txt 
    this is ghostwu

    5,查找包含'you'的行,  /you/ 这是正则表达式, p是打印,要跟n结合起来用

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    hod old are you
    and you
    fine thank you
    come with me!!!
    ghostwu@dev:~/linux/sed$ sed -n '/you/p' ghostwu.txt 
    how are you
    hod old are you
    and you
    fine thank you

    6,匹配$符号结尾的行

    $符号在正则表达式表示行尾,所以要用 转义

    ghostwu@dev:~/linux/sed$ sed -n '/$/p' ghostwu.txt 
    50$

    7,在第一行后面,增加一行“你好啊"

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    hod old are you
    and you
    fine thank you
    come with me!!!
    how much do you have
    50$
    oh, is it?
    yes
    ghostwu@dev:~/linux/sed$ sed '1a 你好啊' ghostwu.txt 
    this is ghostwu
    你好啊
    how are you
    hod old are you
    and you
    fine thank you
    come with me!!!
    how much do you have
    50$
    oh, is it?
    yes

    8,在第一行和第二行的后面,增加一行

    ghostwu@dev:~/linux/sed$ sed '1,2a learning how to use sed command' ghostwu.txt this is ghostwu
    learning how to use sed command
    how are you
    learning how to use sed command
    fine thank you

    9,也可以增加多行

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    fine thank you
    ghostwu@dev:~/linux/sed$ sed '1a 你好啊
    很高兴认识你' ghostwu.txt 
    this is ghostwu
    你好啊
    很高兴认识你
    how are you
    fine thank you

    10, c为替换操作,数字的意思,跟上面的a一样,代表行

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    fine thank you
    ghostwu@dev:~/linux/sed$ sed '1,2c hey guy' ghostwu.txt 
    hey guy
    fine thank you
    ghostwu@dev:~/linux/sed$ sed '1c hey guy' ghostwu.txt 
    hey guy
    how are you
    fine thank you

    11, s:替换匹配到的内容, s:替换开始 /is/ 匹配包含is的行   g:全部替换

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    fine thank you
    ghostwu@dev:~/linux/sed$ sed 's/is/IS/' ghostwu.txt 
    thIS is ghostwu
    how are you
    fine thank you
    ghostwu@dev:~/linux/sed$ sed 's/is/IS/g' ghostwu.txt 
    thIS IS ghostwu
    how are you
    fine thank you
    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    fine thank you

    12、-i :修改,插入文件,会影响文件的内容,在最后一行,插入bye bye

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    fine thank you
    ghostwu@dev:~/linux/sed$ sed -i '$a bye bye' ghostwu.txt 
    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    fine thank you
    bye bye

    13,在1-3行,每一行的后面都插入一行数字

    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    how are you
    fine thank you
    bye bye
    ghostwu@dev:~/linux/sed$ sed -i '1,3a 123457' ghostwu.txt 
    ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
    this is ghostwu
    123457
    how are you
    123457
    fine thank you
    123457
    bye bye
  • 相关阅读:
    关于MySQL中的TRUNCATE语句
    关于在如何linux上部署禅道
    关于Python中的for...else...语句格式
    关于python中身份标识"is"与值运算符"=="
    Vite Vue3.0 使用 SVG Icon (自定义Vite插件)
    Python 远程开发树莓派 点亮LED灯
    Vue 基于elementUI的电梯导航
    JavaScript 原生数字千分位格式化函数(多功能的toLocaleString)
    JavaScript IntersectionObserver 图片懒加载及文字动态划线
    JavaScript await 优雅的捕获异常
  • 原文地址:https://www.cnblogs.com/ghostwu/p/9069837.html
Copyright © 2020-2023  润新知