[RESOLVED]help with sed

Hi guys,

I'm trying to write a script to do a fully automated web server installation. I would need a bit of help in trying to custom some of the values in the files. For example at the moment I am trying to replace the values in the php.inifile using
Code:
sed -i 's/memory_limit = 128M/memory_limit = 32M/g' /usr/local/etc/php.ini
This is not working and I gather it is because of the spaces. Could someone help me to work this out? I have the feeling that I might need to use regular expression here but this is on my list of things to learn. Also would I be better using awk? I am new to both of them.

Fred
 
Re: help with sed

No idea what devel/p5-Config-IniFiles is.. so will that be a perl script withing my sh script?
 
Re: help with sed

fred974 said:
No idea what devel/p5-Config-IniFiles is.. so will that be a perl script withing my sh script?
No, you write a Perl script instead of a shell script to modify your php.ini. The module is there so it's easier to work with .ini files as all the processing is done by the module.
 
Re: help with sed

Ha OK got you thank you.
I never written a Perl script before.. is it a big jump from shell scripting?
 
Re: help with sed

Yeaa, thank you @SirDice:)
A bit of googling and not much of a big effort to achieve what I wanted.
The code bellow seem to do the trick
Code:
#!/bin/sh
INIFILE="$/usr/local/etc/php.ini"
perl -p -i -e '
s/.*memory_limit.*/memory_limit = 32M/;
' "$INIFILE"
Could you please tell me if i need to add anything to it?
 
Last edited by a moderator:
To be honest Perl is probably a bit overkill if you're only going to change that one value. I was under the impression you had to do a lot of modifications. In that case I would go with Perl, mainly because I'm used to the language and it's been the "Swiss army chainsaw" for UNIX since forever.

Your search did have some benefit though, you can probably use that same regexp for sed(1), it'll be a lot lighter than loading a complete Perl interpreter.
 
Perl is easier to use for text manipulation because it supports much more powerful operations. But it is not part of the base system.

sed(1) and awk(1) both have individual weaknesses, so much of the time a task requires both of them.

Interactive testing helps:
Code:
% echo "memory_limit = 128M" | sed -e 's/memory_limit = 128M/memory_limit = 32M/g'
memory_limit = 32M
So that works, but be careful. Are those spaces around the = really spaces, or are they tabs? In sed(1) and awk(1), a space is a space.
 
I haven't used much sed(1) but do use a lot of Perl. I can still spend hours on coming up with a proper regexp. This might prove to be a little more resiliant to different spaces, tabs and numbers:
Code:
% echo "memory_limit = 128M" | sed -E 's/memory_limit[[:space:]]*=[[:space:]]*[[:digit:]]+.*/memory_limit = 32M/g'
memory_limit = 32M
% echo "memory_limit=     64M" | sed -E 's/memory_limit[[:space:]]*=[[:space:]]*[[:digit:]]+.*/memory_limit = 32M/g'
memory_limit = 32M
% echo "memory_limit=     2G| sed -E 's/memory_limit[[:space:]]*=[[:space:]]*[[:digit:]]+.*/memory_limit = 32M/M/g'
memory_limit = 32M

It should now work with any spaces or tabs (or the lack thereof) and with any value that happened to be set. Do note the use of -E instead of -e.
 
SirDice said:
To be honest Perl is probably a bit overkill if you're only going to change that one value. I was under the impression you had to do a lot of modifications. In that case I would go with Perl, mainly because I'm used to the language and it's been the "Swiss army chainsaw" for UNIX since forever.

Your search did have some benefit though, you can probably use that same regexp for sed(1), it'll be a lot lighter than loading a complete Perl interpreter.

No you were right, what i shown you above was only a test to see if it work.
I have add a few more values in there since..

Thank you for the advise
 
Can I ask how do you deal with the following:
Code:
s/.*listen.*/listen = /var/run/php-fpm.sock/;
 
The same way, use [[:space:]]* to indicate zero or more spaces (that includes tabs). The regexp .* is zero or more of any character. Doing it properly would be something like this:
Code:
s/^[[:space:]]*listen[[:space:]]*=.*/listen = \/var\/run\/php-fpm.sock\//
Keep in mind that you need to escape the slashes or they will become part of the search command s///.
 
SirDice said:
The same way, use [[:space:]]* to indicate zero or more spaces (that includes tabs). The regexp .* is zero or more of any character. Doing it properly would be something like this:
Code:
s/^[[:space:]]*listen[[:space:]]*=.*/listen = \/var\/run\/php-fpm.sock\//
Keep in mind that you need to escape the slashes or they will become part of the search command s///.

If you want to write that without having to escape slashes you can use a different separator character between the parts of the replace sed(1) command. I used the underscore (_) character here:

Code:
s_^[[:space:]]*listen[[:space:]]*=.*_listen = /var/run/php-fpm.sock/_

Of course, don't pick a character that is used in the pattern or the replacement part because you'd then have to escape it as well.
 
Yes, with slashes you'll quickly get to the "too many slashes" syndrome ;)

It's quite common to see the pipe character used instead;
Code:
s|search|replace|
 
But vertical bars have a special meaning in the shell, as do some of the other characters sometimes used, like exclamation points. sed(1) and Perl use the next character after the match or substitute command as the delimiter, so different characters can be used in different situations. Percent signs are common, and usually don't have other special meanings: sed -e 's%search%replace%'
 
Back
Top