replacing word after multiple spaces

2019-02-24 22:18发布

问题:

I need to replace a word after multiple spaces :

actually the file contains this string :

local   all             all                                     peer

Which I have to change with

local   all             all                                     md5

I tried various combinations , including

sed -i.bak 's/all[\+ ]peer/md5/g'  file

with no luck .

Any idea ?

Thx in advance

回答1:

Try this (using GNU sed-Version 4.2.1):

sed -E -i.bak 's/(all\s+)peer/\1md5/' file


回答2:

GNU sed

sed -r -i.bak '/^local\s+(all\s+){2}peer$/s/\S+$/md5/' file


回答3:

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


回答4:

sed 's/\(all *all  *\).*/\1md5/'


回答5:

perl -pe 's/(all[\s]+)peer/$1md5/g' your_file


回答6:

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