SED help

Scott Ruckh sruckh at gemneye.org
Wed Feb 13 01:30:05 UTC 2008


This is what you said Herta Van den Eynde
> On 13/02/2008, Scott Ruckh <sruckh at gemneye.org> wrote:
>> I would like to do the following SED replacement, but I am not having
>> much
>> luck and thought I would ask for help.
>>
>> TEXT1="some-text a.b.c.d a.b.c.d a.b.c.d a.b.c.d"
>> TEXT2="some-text 1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4"
>>
>> sed -i "s/$TEXT1/$TEXT2/g" $INFILE
>>
>> ;a.b.c.d are unknown IP Addresses.
>>
>> ----------------------
>>
>> I have tried this:
>>
>> TEXT2="some-text 1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4"
>> IP_REGEX="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][$
>> TEXT1="^some-text[ \t]${IP_REGEX}[ \t]${IP_REGEX}[ \t]${IP_REGEX}[
>> \t]${IP_REGEX}"
>>
>> sed -i "s/$TEXT1/$TEXT2/g" $INFILE
>>
>> ----------------------
>>
>> What I have does not work, and I need some assistance.
>>
>> Your help is appreciated.
>>
>> Thanks.
>>
> I think you simply missed out on the option: -e instead of -i.  This
> works for me:
>
> TEXT1="some-text a.b.c.d a.b.c.d a.b.c.d a.b.c.d"
> TEXT2="some-text 1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4"
>
> sed -e "s/$TEXT1/$TEXT2/g" $INFILE
>
Here is the poorly coded version I ended up with...

This is going to be used to dynamically update my openvpn configuration
server configuration.

#!/bin/sh

OUTFILE=/etc/openvpn/eth.txt
SED=`which sed`
PERL=`which perl`
OVPN_CFG=/etc/openvpn/mytest.txt

eth_ip=`/sbin/ifconfig  | /bin/grep 'inet addr:'| /bin/grep -v '127.0.0.1'
| /usr/bin/cut -d: -f2 | /usr/bin/awk '{print $1}'`

echo $eth_ip > $OUTFILE

oct1=`/usr/bin/cut -d. -f1 $OUTFILE`
oct2=`/usr/bin/cut -d. -f2 $OUTFILE`
oct3=`/usr/bin/cut -d. -f3 $OUTFILE`
CLASS_C="${oct1}.${oct2}.${oct3}"

rm -f $OUTFILE

BRIDGE_STRING="server-bridge ${eth_ip} 255.255.255.0 ${CLASS_C}.30
${CLASS_C}.35"
IP_REGEX="\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"
BRIDGE_REGEX="server-bridge[ \t]${IP_REGEX}[ \t]${IP_REGEX}[
\t]${IP_REGEX}[ \t]${IP_REGEX}"

${PERL} -pi -e "s/${BRIDGE_REGEX}/${BRIDGE_STRING}/g" $OVPN_CFG




More information about the redhat-list mailing list