Linuxスクリプトでsedを使用してファイルの内容を変更するさまざまなテクニック


Linuxスクリプトでsedを使用してファイルの内容を変更する
       
 a   ,            。            “\”  (     )
 c   ,                  。            “\”  (     )
 d   ,   
 i   ,          。            “\”  (     )
 h                 
 g           ,            
 l        
 n        ,                      
 p    
 q      sed
 r          
 !               
 s   ,           。            :1,$ s/old/new/g
 g          
 w          
 x                
 y           (          y  )

       
 -e       ,         sed     
 -n        ,    (silent)sed     ,     STDIN              。      -n    ,     sed         (    )      
 -f   sed      
 -i             ,         
 -r  sed                  ,   \      -r
  • 直接前の例、説明方法:
  • root@localhost test]# ls
    test.sh
    [root@localhost test]# vim test.sh 
    #!/bin/sh
    
    
    file="a.conf"
    newip=$1
    
    if [ $# != 1 ]; then
      echo "please use: $0 "
      exit
    fi
    
    content="
    #config file
    
    IPADDR:$newip
    MASK:24
    PRO:IPv4
    "
    #modify ip addr
    if [ -e "$file" ]; then
      #file already exist, modify
      sed -i "s/^IPADDR:.*$/IPADDR:$newip/g" $file
    else
      #file not found, new file
      echo "$content" > $file
    fi
    
    #delete all lines contain "AAA_add_info_into_line2"
    sed -i '/AAA_add_info_into_line2/d' $file
    
    #append info "AAA_add_info_into_line2" into line 2
    sed -i '2a AAA_add_info_into_line2' $file
    
    #append info "BBB_add_info_into_the_last_line" into the last line
    sed -i '$a BBB_add_info_into_the_last_line' $file
    
    #append info "CCC_add_info_into_file_after_the_fix_line" after the line start with "MASK:24"
    sed -i '/^MASK:24/a CCC_add_info_into_file_after_the_fix_line' $file
    
    #find all lines start with "MASK:24" and join with " #or 255.255.255.0 is valid."
    sed -i 's/^MASK:24/& #or 255.255.255.0 is valid./' $file
    ~                                                                                                                                                                          
    
    [root@localhost test]# ./test.sh 
    please use: ./test.sh <ip>
    [root@localhost test]# ./test.sh  1.1.1.1
    [root@localhost test]# cat a.conf 
    
    #config file
    AAA_add_info_into_line2
    
    IPADDR:1.1.1.1
    MASK:24 #or 255.255.255.0 is valid.
    CCC_add_info_into_file_after_the_fix_line
    PRO:IPv4
    
    BBB_add_info_into_the_last_line
    [root@localhost test]# 
    [root@localhost test]# ./test.sh 2.2.2.2
    [root@localhost test]# 
    [root@localhost test]# cat a.conf 
    
    #config file
    AAA_add_info_into_line2
    
    IPADDR:2.2.2.2
    MASK:24 #or 255.255.255.0 is valid. #or 255.255.255.0 is valid.
    CCC_add_info_into_file_after_the_fix_line
    CCC_add_info_into_file_after_the_fix_line
    PRO:IPv4
    
    BBB_add_info_into_the_last_line
    BBB_add_info_into_the_last_line
    [root@localhost test]# 
    
  • 行の一部(非整行)sed 's/ / /g'
  • を置き換える.
  • 式は正則
  • をサポートする
  • 一致行の文字列sed -i '/ /s/ / /g' $file
  • を置換する.
    # sed -i "/bin\/mysqld\ \-\-initialize/s/\-\-basedir=[^ ]*\ /\-\-basedir=\/usr\/local\/mysql\ /g" $file
    
    # sed -n '/abc/p' $file | sed 's/abc/newABC/g'
    # sed -n '/abc/p' $file | sed 's/abc//g'
    #             "END"
    # sed -i '$a END' $file
    
    #          :
    sed 's/'"'"'//g'
    or
    sed 's/'\''//g'
    or
    sed s/\'//g