Reputation: 708
I try to use regex in a sed command. I try to replace a pattern. My pattern is a line in /etc/security/policy.conf file. The value is unknown it should be either:
#LOCK_AFTER_RETRIES=NO or LOCK_AFTER_RETRIES=4 (or any number here). I want to replace found pattern with the value LOCK_AFTER_RETRIES=6. So I tried the command:
sed 's/#*LOCK_AFTER_RETRIES=[a-zA-Z0-9][a-zA-Z0-9]/LOCK_AFTER_RETRIES=6/g' ./policy.conf
But it didn't look to be working.
Thanks.
Upvotes: 1
Views: 379
Reputation: 3022
Is this what you want?
sed -i 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g' filename
I test like this and it works:
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
[EDIT]
After checking other's answer, I am not sure whether you want to replace #LOCK_AFTER_RETRIES=NO
to LOCK_AFTER_RETRIES=6
or #LOCK_AFTER_RETRIES=6
.
My above answer will change #LOCK_AFTER_RETRIES=NO
to #LOCK_AFTER_RETRIES=6
If you want to change change #LOCK_AFTER_RETRIES=NO
to LOCK_AFTER_RETRIES=6
, try:
sed -i 's/^#*LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g' filename
In summary:
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
LOCK_AFTER_RETRIES=6
#LOCK_AFTER_RETRIES=6
echo "LOCK_AFTER_RETRIES=4\n#LOCK_AFTER_RETRIES=NO" | sed 's/^#*LOCK_AFTER_RETRIES=.*/LOCK_AFTER_RETRIES=6/g'
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
Upvotes: 1
Reputation: 195079
give this a try:
sed 's/^#\?\s*LOCK_AFTER_RETRIES=[^=]*/LOCK_AFTER_RETRIES=6/'
test with some example:
# LOCK_AFTER_RETRIES=NO
#LOCK_AFTER_RETRIES=NO
LOCK_AFTER_RETRIES=4
LOCK_AFTER_RETRIES=4
kent$ sed 's/^#\?\s*LOCK_AFTER_RETRIES=[^=]*/LOCK_AFTER_RETRIES=6/' f
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
LOCK_AFTER_RETRIES=6
Upvotes: 2