replacing word after multiple spaces

后端 未结 6 1894
甜味超标
甜味超标 2021-01-20 08:46

I need to replace a word after multiple spaces :

actually the file contains this string :

local   all             all                                         


        
相关标签:
6条回答
  • 2021-01-20 09:11

    Try this (using GNU sed-Version 4.2.1):

    sed -E -i.bak 's/(all\s+)peer/\1md5/' file
    
    0 讨论(0)
  • 2021-01-20 09:17

    Try this one:

    sed -i.bak -e "s/\(all[ ]\+\)peer/\1md5/g" file
    

    You need to escape ()+ characters. Space could be like: [ ] with \+ you say that it should be more than 0. Replace it with * if in some cases you do not have a space between "all" and "peer"

    In the replace part you should use \1 - back-reference to the first found part: all, and only peer will be replaced with md5

    0 讨论(0)
  • 2021-01-20 09:26

    Am I missing some part of this problem? I made a file with the first line above as contents, then ran the simplest possible sed command on it ... and came out with the 2nd line.

    ~ >cat test.txt
    local   all             all                                     peer
    
    ~ >sed 's/peer/md5/g' test.txt
    local   all             all                                     md5
    

    works fine with multiple lines as well ...

    ~ >cat test.txt
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    ~ >sed 's/peer/md5/g' test.txt
    local   all             all                                     md5
    local   all             all                                     md5
    local   all             all                                     md5
    local   all             all                                     md5
    local   all             all                                     md5
    local   all             all                                     md5
    

    The only time this will not work is if the word "peer" shows up somewhere else on a line where you do not want it replaced with md5 ...

    This one replaces only the first occurance of the search term:

    ~ >cat test.txt
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    ~ >sed '0,/peer/s//md5/' test.txt
    local   all             all                                     md5
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    local   all             all                                     peer
    
    0 讨论(0)
  • 2021-01-20 09:26
    perl -pe 's/(all[\s]+)peer/$1md5/g' your_file
    
    0 讨论(0)
  • 2021-01-20 09:28
    sed 's/\(all *all  *\).*/\1md5/'
    
    0 讨论(0)
  • 2021-01-20 09:29

    GNU sed

    sed -r -i.bak '/^local\s+(all\s+){2}peer$/s/\S+$/md5/' file
    
    0 讨论(0)
提交回复
热议问题