sed replace does not come up with my desired result - replace

sudo sed -i 's!# dbdir /var/lib/munin!dbdir /var/lib/munin!g' /etc/munin/munin.conf
sudo sed -i 's!localhost 127.0.0.0/8 ::1!all!g' /etc/munin/apache.conf
Why does # dbdir /var/lib/munin does not get replace with dbdir /var/lib/munin
and
why does localhost 127.0.0.0/8 ::1 not get replaced with all?
sudo sed -i 's!# dbdir!dbdir!g' /etc/munin/munin.conf
gives a satisfactory result, only the localhost replacement question remaining.

In my munin.conf there are multiple space between dbdir and /var/lib/munin so unless you have exact info this replace would not work.
You search for only part of the text then replace the line:
awk '/dbdir/ {$0="dbdir /var/lib/munin"}1' /etc/munin/munin.conf > temp ; mv temp /etc/munin/munin.conf
or remove then # in front of the line
awk '/dbdir/ {sub(/^#/,x)}1' /etc/munin/munin.conf
EDIT:
awk '/Allow from local/ {sub(/localhost 127.0.0.0\/8 ::1/,"all")}1' /etc/munin/apache.conf

Related

Hard regex with sed

In a script.sh file, I have the following line:
ExecStart=ssh -nN -R 46:192.168.0.1:56 192.168.0.2
I try to replace with sed the second port (56 here) knowing that its value can vary between 1 and 65535.
So I tried that without success :
sed -i -e "s/:.*[[:space/]]/other port number/2g' script.sh
Could you help me solve my regex?
You may use:
sed -i "s/:[0-9]\{1,5\} /:other port number /" script.sh
$ other_port_number="123"
$ echo "ExecStart=ssh -nN -R 46:192.168.0.1:56 192.168.0.2" | sed "s/:[0-9]\{1,5\} /:$other_port_number /"
ExecStart=ssh -nN -R 46:192.168.0.1:123 192.168.0.2

Getting delimited substrings using sed+regexp

I'm trying to get substrings using sed with regexp. I want to get the first and second "fields" delimited by ":".
To get the first field I used the following command, but don't know how to get the second field.
Command used to get the first field:
sed -r -n '1,2 s/([^:]+).*/\1/p' /etc/passwd
Input file (example):
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
Command's result:
root
daemon
But I tried do get the first ("root") and second ("x") fields (examples based on the file's first line only), but I did't succedded.
I tried:
sed -r -n '1,2 s/([^:]+).*([^:]+).*/\1 \2/p' /etc/passwd
Command's result:
root h
daemon n
Desired result:
root x
daemon x
sed uses greedy match. In
sed -r -n '1,2 s/([^:]+).*([^:]+).*/\1 \2/p' /etc/passwd
^^
.* matches as many characters as possible. You need
sed -r -n '1,2 s/([^:]+):([^:]+).*/\1 \2/p' /etc/passwd
^
Demo: http://ideone.com/wjL7Za.
By the way, a simpler way to do this is using cut:
cut -d ":" -f 1,2 --output-delimiter=' ' /etc/passwd
Demo: http://ideone.com/stJdSy.
Another expression that would return the desire result would be:
([a-z]+):([a-z]+).*
RegEx Demo
sed -r -n '1,2 s/([^:]+):([^:]+).*/\1 \2/p'
Sed Demo

sed command to replace php.ini memory_limit

I have php.ini with memory_limit = 128M
I would like to replace this with sed command.
I had try this, and it does not work
sed -i '' 's/memory_limit\s*=\s*\d*M/memory_limit = 1024M/g' ~/Desktop/php.ini
any idea why?
I copy it to desktop so it does not tinker the original one before it actually working
I have modified your regex a little, also removed your empty single strings and it's working fine to me.
I changed this:
sed -i '' 's/memory_limit\s*=\s*\d*M/memory_limit = 1024M/g' ~/Desktop/php.ini
To this:
sed -i 's/memory_limit\s*=.*/memory_limit=1024M/g' ~/Desktop/php.ini
Regex change here--^^
Console output
$ cat php.ini
asdfasd
memory_limit = 128M
fasd
$ sed -i 's/memory_limit\s*=.*/memory_limit=1024M/g' ~/php.ini
$ cat php.ini
asdfasd
memory_limit=1024M
fasd
Btw, my sed version is sed (GNU sed) 4.2.2
The BSD sed on Mac OS X does not recognize \s as space and \d as digit even with the -E option:
$ echo 'memory_limit = 128M' | sed -E 's/memory_limit\s*=\s*\d*M/memory_limit = 1024M/g'
memory_limit = 128M
$ # Oops - output is the same as the input!
I didn't manage to create an input string that is recognized by the s/// you show, with or without the -E option (which surprises me a little; I tried a fair number of variations). However, the substitution you require can be done portably and easily with:
$ echo 'memory_limit = 128M' |
> sed 's/memory_limit[[:space:]]*=[[:space:]]*[[:digit:]]*M/memory_limit = 1024M/g'
memory_limit = 1024M
$
Fixing that so it does the in-place edit of the file without any backup (I hope you have the file under some sort of configuration management!) is a trivial combination of what I wrote and what you wrote.
If you use -i.bak (creating a backup; no space between -i and .bak), then the script will work with GNU sed as well as BSD sed. If you don't do the overwriting, then it will work with any POSIX-compliant sed.
This solution will work on OSX:
sed -Ei '' 's/(memory_limit = )[0-9]+M/\11024M/g' ~/Desktop/php.ini
i key for edit file in-place
E key for extended regex
'' need to supply when no .bak (backup) specified
s substitute pattern with
()defining a group
\1 group reference
[0-9]+M - any digit with M at the end
g - substitute all occurrences of the pattern
It's always good to save backup file:
sed -i .bak -E 's/(memory_limit = )[0-9]+M/\11024M/g' php.ini
This will save you a copy of php.ini before substitution to php.ini.bak

Sed: How to replace a string which includes multiple special characters?

I'm trying to comment a line in /etc/sudoers through a shell script.
That's the relevant line I'd like to edit:
# grep '\!requiretty' /etc/sudoers
Defaults:nagios !requiretty
But it seems like the pattern I'm using with sed is incorrect, my tries:
# sed -i 's/^Defaults\:nagios$/#Defaults:nagios !requiretty/g' /etc/sudoers
# sed -i 's/^Defaults:nagios$/#Defaults:nagios !requiretty/g' /etc/sudoers
# sed -i 's/^Defaults:nagios$/#Defaults:nagios !requiretty/g' /etc/sudoers
# sed -i 's/^Defaults:nagios$/\#Defaults:nagios !requiretty/g' /etc/sudoers
# sed -i 's/^Defaults:nagios$/\#Defaults:nagios \!requiretty/g' /etc/sudoers
# sed -i 's/^Defaults:nagios$/^#Defaults:nagios !requiretty/g' /etc/sudoers
# sed -i 's/^Defaults\:nagios$/^#Defaults:nagios !requiretty/g' /etc/sudoers
# sed -i 's/^Defaults\:nagios$/#Defaults:nagios \!requiretty/g' /etc/sudoers
None of the above worked...
Can someone please assist me with the correct regex?
Thanks in advance
Well ... sudoers is not meant to be writable by anyone -- even root. You're supposed to edit it with the visudo command instead for security reasons.
I think you might have it backwards though since the first part of the sed substitution is the find. The second part is the replacement. So you would want to do something like:
sed -i 's/^Defaults:nagios !requiretty$/#Defaults:nagios/'
This will also remove the requiretty. If all you want is to add the # you could just do:
sed -i 's/^Defaults:nagios !requiretty$/#&/'

Regular expression required for replacing string in shell script

Can anyone please help me write a shell script in linux which would replace the hostname in a particular file.
eg : I have multiple files which have certain ip addresses.
http://10.160.228.12:8001/soa-infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL
http://VQAIAAPPDEV:8001/soa-infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL
Basically what I would want to replace is the string between "http://" and ":8001" with any required string.
Can someone help me with this please.
Some More info:-
I want to do this iteratively across many folders. So basically it will search all the files in each folder and perform the necessary changes.
You could use sed. Saying:
sed -r 's|(http://)([^:]*)(:8001)|\1something\3|g' filename
would replace is the string between "http://" and ":8001" with something.
If you want to make the change to the file in-place, use the -i option:
sed -i -r 's|(http://)([^:]*)(:8001)|\1something\3|g' filename
Use sed command from Linux shell
sed -i 's%OldHost%NewHost%g' /yourfolder/yourfile
Tried with "for"
# cat replace.txt
http://10.160.228.12:8001/soa- infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL
http://VQAIAAPPDEV:8001/soa-infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL
# for i in `cat replace.txt | awk -F: '{print $2}' | sed 's/^\/\///g' | sed '/^$/d'` ; do sed -i "s/$i/Your_hostname/" replace.txt ; done
# cat replace.txt
http://Your_hostname:8001/soa- infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL
http://Your_hostname:8001/soa-infra/services/default/AIAAsyncErrorHandlingBPELProcess/client?WSDL
Its working for me...!