2

I need to change a directive in a config file and got it working in Linux but in Solaris, it says command garbled.

Here is the directive

    enable-cache            passwd          yes

I need to simply change the yes to no. How can I do with with sed that will work for Solaris, HPUX and Linux?

Here is the sed command that worked in Linux. Solaris doesn't like the -r

sed -r 's/^([[:space:]]*check-files[[:space:]]+passwd[[:space:]]+)yes([[:space:]]*)$/\1no\2/' inputfile

The end goal is to put this command in a script and run it across the enterprise.

Thanks

Greg

I also posted something similar yesterday which worked for Linux but not for the others.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
user1967720
  • 155
  • 1
  • 2
  • 7

3 Answers3

2

Solaris has /usr/bin/sed and /usr/xpg4/bin/sed. None of these support an -r option, which option for Linux is to use an extended regex. sed in Solaris does not have any option to set the regex like that. You can use other tools, specifically awk, if you want simpler portability. Or you will have to use two flavors of regex, one with -r and an extended regex, one without -r and a different regex. And you probably want to specify /usr/xpg4/bin/sed on Solaris boxes only:

#!/bin/bash
sun=`expr index Solaris $(uname -a)`
if [ $sun -ne 0 ] ; then
   /usr/xpg4/bin/sed [longer regex here ] 
else
  /usr/bin/sed -r [ extended regex here ]
fi 
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
0

This is not strictly equivalent as :space: match more characters but I assume only space and tab are to be expected in your file. I'm only using standard shell and sed commands so this should work on Solaris, Linux and HP-UX:

space=$(printf " \t")
sed 's/^\(['"$space"']*check-files['"$space"']+passwd['"$space"']+\)yes\(['"$space"']*\)$/\1no\2/' inputfile

Note that your script doesn't match your sample directive as it expects check-files but is given enable-cache.

jlliagre
  • 29,783
  • 6
  • 61
  • 72
0

The GNU [[:space:]] is usually equivalent to just [ \t]. And you need to escape the parentheses. And + is not supported. So with these replacements, your working sed command becomes:

sed 's/^\([ \t]*check-files[ \t][ \t]*passwd[ \t][ \t]*\)yes\([ \t]*\)$/\1no\2/' inputfile

Further note: The older sed's don't have a -i option for doing in-place changes, so you might first have to copy your target to a temporary file, and apply sed to it, redirecting the output to the target.

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77