且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

learn Linux sed command

更新时间:2022-08-12 21:16:37

                                  learn Linux sed command


一、参考文档:
    1. sed命令详解
        http://qifuguang.me/2015/09/21/sed%E5%91%BD%E4%BB%A4%E8%AF%A6%E8%A7%A3/
    2. linux之sed用法
        http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html
    3. Sed 的man手册参数详细解释(一)
        http://blog.csdn.net/imfinger/article/details/6071175

二、sed命令的使用规则是这样的:
    sed [option] 'command' input_file

三、options:
    -n, --quiet, --silent
        suppress automatic printing of pattern space
    -e script, --expression=script
        add the script to the commands to be executed
        如果命令行上只有一个指令的时候可以不用写-e,但是如果有多个指令的话一定要在每个指令的前面加-e选项。
    -f script-file, --file=script-file
        add the contents of script-file to the commands to be executed
        这里貌似是前面对应的-e对应的脚本
    --follow-symlinks
        follow symlinks when processing in place
    -i[SUFFIX], --in-place[=SUFFIX]
        edit files in place (makes backup if extension supplied)
    -l N, --line-length=N
        specify the desired line-wrap length for the `l' command
    --posix
        disable all GNU extensions.
    -r, --regexp-extended
        use extended regular expressions in the script.
    -s, --separate
        consider  files  as  separate rather than as a single continuous
        long stream.
    -u, --unbuffered
        load minimal amounts of data from the input files and flush  the
        output buffers more often
    --help
        display this help and exit
    --version
        output version information and exit

四、command有以下几种:
    a \: append即追加字符串, a \的后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选择的行的后面都加上字符串s;
    c \: 取代/替换字符串,c \后面跟上字符串s(多行字符串可以用\n分隔),则会将当前选中的行替换成字符串s;
    d  : delete即删除,该命令会将当前选中的行删除;
    i \: insert即插入字符串,i \后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选中的行的前面都插入字符串s;
    p  : print即打印,该命令会打印当前选择的行到屏幕上;
    s  : 替换,通常s命令的用法是这样的:1,2s/old/new/g,将old字符串替换成new字符串