Linux Three Musketeers~Usage of sed command

1. Working principle:

  • sed is a stream editor which is a very useful tool in text processing

  • It can be used perfectly with regular expressions. During processing, the currently processed line is stored in a temporary buffer, called the pattern space, and then the sed command is used to process the content in the buffer.

  • After processing is completed, the contents of the buffer are sent to the screen. Then process the next line, and repeat until the end of the file. The file content has not changed

  • Features of sed:

    • The sed command is ideal for applying a series of editing commands to a batch of text

    • The sed command is a non-interactive text editor that can edit text from text files and standard input. Among them, standard input can be text from the keyboard, file redirection, string, variable or pipe.

    • The sed command reads one line of data at a time from the file or standard input, copies it to a buffer (up to 8192 bytes), and then reads the command line or script's editing subcommand to edit the text lines in the buffer. Repeat this process until all lines of text have been processed

2.sed basic syntax:

  • grammar:

    sed OPTIONS… [SCRIPT] [INPUTFILE…]

  • parameter:

    Commonly used options illustrate
    -n,--quiet,--silent Print only matching lines
    -i Edit the original file directly instead of outputting it on the screen. The original file will not be operated by default;
    -It is Perform sed action editing directly on the command line mode without modifying the original file.
    -r Use extended regular expressions
    -f Directly write the sed action in a file. -f filename can execute the sed action in filename.
    [root@quruixiang ~]# sed -n '3p' /etc/passwd

3. Editing operations in pattern space:

  • Address delimitation: (query)

    # 1)#:#为数字,指定要进行处理操作的行;1,表示第一行;
    [root@quruixiang ~]# sed -n '3,4p' /etc/passwd
    # 2)$:表示最后一行,多个文件进行操作的时候,为最后一个文件的最后一行;
    [root@quruixiang shellstudy]# sed -n '$p' test8.sh
    # 3)/regexp/:表示能够被regexp匹配到的行;
    [root@quruixiang ~]# sed -n "/test/p" /etc/passwd
    ## regexp即基于正则表达式的匹配;
    # 4)/regexp/I:匹配时忽略大小写;
    [root@quruixiang shellstudy]# sed -n "/test/Ip" /etc/passwd
    # 5)\%regexp%: 任何能够被regexp匹配到的行,换用%(用其他字符也可以,如:#)为边界符号;
    [root@quruixiang shellstudy]# sed -n "\%echo%p" test8.sh
    # 6)addr1,addr2:指定范围内的所有的行(范围选定);
    [root@quruixiang ~]# sed -n "3,6p" /etc/passwd
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    # 常用地址定界表示方式:
    ## a)0,/regexp/:从起始行开始到第一次能够被regexp匹配到的行。
    [root@quruixiang shellstudy]# sed -n "0,/bin/p" test8.sh 
    ## b)/regexp/,/regexp/:被模式匹配到的行内的所有的行。
    ## 7)first~step:指定起始的位置及步长,例如:1~2表示1,3,5…
    [root@quruixiang shellstudy]# sed -n "1~3p" test8.sh 
    #!/bin/bash
    #Version:v1.0
    #Description:
    func()
            echo 1
        fi    
    echo $result
     
    ## 8)addr1,+N:指定行以及以后的N行;
    [root@quruixiang ~]# sed -n "3,+4p" /etc/passwd
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    ### addr1,~N:addr1开始的行,N结束的行
    [root@quruixiang ~]# sed -n "3,~4p" /etc/passwd
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin

  • Precautions:

    • If no address is specified, the command will be applied to each line

    • If there is only one address, it means that the command will be applied to all lines matching this address.

    • If two addresses separated by a comma are specified, it means that the command should be applied to the lines matching the first address and the second address (inclusive)

    • If an address is followed by an exclamation mark, it means that the command will be applied to all lines that do not match the address.

  • Commonly used editing commands: (edit)

    # 1)d:删除匹配到的行
    [root@quruixiang shellstudy]# sed '3d' test8.sh
    [root@quruixiang shellstudy]# sed '/echo/d' test8.sh
    [root@quruixiang shellstudy]# sed '/^$/d' test8.sh     # 删除空行
    # 2)p:打印当前模式空间内容
    [root@quruixiang shellstudy]# sed '/echo/p' test8.sh
    # 3)a \text:append,表示在匹配到的行之后追加内容
    [root@quruixiang shellstudy]# sed '$a\system' test8.sh
    [root@quruixiang shellstudy]# sed '3a\systemctl' test8.sh
    [root@quruixiang shellstudy]# sed '/echo/a\systemctl' test8.sh
    # 4)i \text:insert,表示在匹配到的行之前追加内容
    [root@quruixiang shellstudy]# sed '3i\sys' test8.sh
    [root@quruixiang shellstudy]# sed '/echo/i\sys' test8.sh
    # 5)c \text:change,表示把匹配到的行和给定的文本进行交换
    [root@quruixiang shellstudy]# sed '/echo/c\system' test8.sh
    # 6)s/regexp/replacement/flages:查找替换,替换regexp匹配到的内容(其中/可以用其他字符代替,
    sed -i '/^SELINUX=/ s/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config 
    [root@quruixiang shellstudy]# sed 's/fun/##/1' test8.sh      # 替换每行中第一个匹配的fun替换为##
        ## 例如@)
        ## 其他编辑命令:
        ## 常用的flages:
        ## g:全局替换,默认只替换第一
        [root@quruixiang shellstudy]# sed 's/echo/system/g' test8.sh
        [root@quruixiang shellstudy]# sed 's/^/##/g' test8.sh        # 文件中的每一行开头加一个##
        [root@quruixiang shell]# sed -i 's/^#[[:space:]]*//g' test.txt      # 删除开头#和至少一个空字符的行 
        ## i: 不区分大小写
        ## p:如果成功替换则打印
        [root@quruixiang shellstudy]# sed 's/echo/system/p' test8.sh
        
    # 7)r 读入文件内容追加到匹配行后面
    [root@quruixiang shellstudy]# sed  'r 2.txt'  1.txt        # 在1.txt中的每一行后都写入2.txt的内容
    [root@quruixiang shellstudy]# sed '3r 2.txt'  1.txt        # 在1.txt中的第3行后写入2.txt的内容
    [root@quruixiang shellstudy]# sed  '/245/r 2.txt'   1.txt    # 在1.txt中的匹配行后写入2.txt的内容
    # 8)R 读入文件一行内容追加到匹配行后面
    # 9)y :y/source/dest/ 固定长度替换,要求替换的字符串长度相等
    # 10)w /path/to/somefile:将匹配到的文件内容追加到指定的文件末尾 
    [root@quruixiang shell]# sed -n 'w test.txt' passwd        # 在test.txt的末尾0后写入passwd的内容

4.sed extension:

special symbols illustrate
Applies command to all lines except specified lines
= Print the current line number
~ "first~step" means starting from the first line and increasing by step.
& Represents the content to be replaced
; Implement one line of command statements to execute multiple sed commands
{} Perform bulk operations on a single address or address range
+ Symbols used in the address range for addition operations
# 打印匹配到echo的行号
[root@quruixiang shellstudy]# sed -n '/echo/=' test8.sh
# 打印最后一行的行号
[root@quruixiang shellstudy]# sed -n '$=' test8.sh 
# 删除3之外的所有行
[root@quruixiang shellstudy]# sed '3!d' test8.sh    
# 删除1~3之外的所有行
[root@quruixiang shellstudy]# sed '1,3!d' test8.sh
# 删除匹配到fi到最后一行之外的所有行
[root@quruixiang shellstudy]# sed '/fi/,$!d' test8.sh
# 删除从匹配fi的行到最后一行
[root@quruixiang shellstudy]# sed '/fi/,+1d' test8.sh
# 删除匹配echo或fi的行
[root@quruixiang shellstudy]# sed '/echo\|fi/d' test8.sh
# 删除1~3行中,匹配内容bin的行
[root@quruixiang shellstudy]# sed '1,3{/bin/d}' test8.sh
# 打印匹配echo的行的行号和内容
[root@quruixiang shellstudy]# sed -n '/echo/{=;p}' test8.sh
# 打印3行到10行的内容
[root@quruixiang shell]# sed -n '3,10{=;p}' test.txt 
 

Guess you like

Origin blog.csdn.net/qq_56776641/article/details/133943511