I need to replace a word after multiple spaces :
actually the file contains this string :
local all all
Try this (using GNU sed-Version 4.2.1
):
sed -E -i.bak 's/(all\s+)peer/\1md5/' file
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
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
perl -pe 's/(all[\s]+)peer/$1md5/g' your_file
sed 's/\(all *all *\).*/\1md5/'
sed -r -i.bak '/^local\s+(all\s+){2}peer$/s/\S+$/md5/' file