1

I'm trying to perform a string replacement on several hundred awstats config files, and using sed to do so. However, I'm having trouble getting a working pattern.

To be specific, I'm trying to convert this line in the configs:

HostAliases="REGEX[^\.*example\.com$]"

into this:

HostAliases="REGEX[\.example\.com$]"

where example\.com is variant.

I've tried dozens of variations of sed command. For example:

sed -i 's/^HostAliases="REGEX[^\.*/HostAliases="REGEX[\./' /etc/awstats/*.conf

sed -i 's/REGEX[^\.*/REGEX[\./' /etc/awstats/*.conf

but each time get an error like: sed: -e expression #1, char 49: unterminated 's' command

The issue is probably to do with escaping some of the characters but I can't figure out which ones or how to escape them. I want to make the pattern specific enough to avoid accidentally matching other lines in the config, so including the HostAliases would be good.

Could some sed guru please point me in the right direction?

Martijn Heemels
  • 3,529
  • 5
  • 37
  • 38

4 Answers4

3

The first argument to 's' is itself a regular expression. So you have to escape (with \) the special characters. In your case they are [, \, . and *. So it should be:

sed -i 's/^HostAliases="REGEX\[^\\\.*/HostAliases="REGEX[\./' /etc/awstats/*.conf
sed -i 's/REGEX\[^\\\.\*/REGEX[\./' /etc/awstats/*.conf
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • Thanks, your answer got me thinking the right way, so I could fix my own problem. The actual sed command I needed was `sed -i 's/^HostAliases="REGEX\[^\\\.\*/HostAliases="REGEX[\\./' /etc/awstats/*.conf`. I'm awarding point for best explanation. – Martijn Heemels Apr 13 '11 at 20:32
1

Is this what you mean?

$ echo 'HostAliases="REGEX[^\.*example\.com$]"' | sed 's/^HostAliases=\"REGEX\[\^\\\.\*/HostAliases="REGEX[\\\./'

Outputs:

HostAliases="REGEX[\.example\.com$]"
AlG
  • 14,697
  • 4
  • 41
  • 54
1

You need to escape special regular expression characters too, like this:

sed -i 's/REGEX\[\^\\.\*/REGEX[\\./' /etc/awstats/*.conf
markshep
  • 728
  • 7
  • 17
0

You forgot to escape some chars, like ^ and [ . Your sed would be something like this:

sed -e 's/REGEX\[\^\.*/REGEX\[/' -i /etc/awstats/*conf
ThoriumBR
  • 930
  • 12
  • 25