简体   繁体   中英

sed command not working properly in solaris but working in linux

I have following string which is used further in sed command. Its working properly in Linux but NOT working in Solaris

-bash-3.00$ string="CREATESETTABLEDATABASE1.TABLE1(uid)CREATESETTABLEDATABASE1.TABLENAMEuid,cid,mid)DATABASE2.TABLENAME(hi,hello)"

In Linux box, it outputs properly as below.

echo $string | sed -e 's/.*CREATESETTABLE[^)]\+TABLENAME\(.*\)/\1/g'
uid,cid,mid)DATABASE2.TABLENAME(hi,hello)

I solaris , sed search is not working returns full string irrespective of search string match.

echo $string | sed -e 's/.*CREATESETTABLE[^)]\+TABLENAME\(.*\)/\1/g'
CREATESETTABLEDATABASE1.TABLE1(uid)CREATESETTABLEDATABASE1.TABLENAMEuid,cid,mid)DATABASE2.TABLENAME(hi,hello)

I want the same output to be printed in solaris.

I believe \\+ doesn't work on older sed even on BSD it is not supported. Try this sed:

sed -e 's/.*CREATESETTABLE[^)]*TABLENAME\(.*\)/\1/g'

POSIX sed supports only BRE (basic regular expressions) in which + has no special meaning .

One important oddity (relatively speaking) about BREs is that () and {} require \\ -escaping in order to gain their special meaning. Those characters, and only those characters, require such escaping. The opposite is required in contemporary (ERE) expressions, \\ -escaping them is required to disable their special meaning.

The behaviour of an escaped ( \\ ) non-special character in a BRE is undefined by the specification.

You problem stems from the fact that \\+ (along with \\? , and \\| within \\(\\) ) are GNU extensions .

These BRE extensions preserve the convention of a \\ prefix, but when GNU sed is given the option -r it will enable ERE (extended regular expressions) in which + has its modern meaning (equivalent to {1,} ) and the requirement for the extra \\ is removed. Similarly, standard BREs have no special meaning for ? (or \\? , equivalent to {0,1} ), this feature is also enabled with -r .

If you use the GNU sed --posix option this will disable the various GNU extensions, and your scripts should in general be more portable (though perhaps more convoluted). Well nearly, prior to GNU sed 4.2 (April 2009) the --posix option did not disable all the BRE extensions, you should make sure to use an up to date version so that non-POSIX features don't creep in.

The most portable way to achieve what you want is with {1,} :

echo $string | sed --posix -e 's/.*CREATESETTABLE[^)]\{1,\}TABLENAME\(.*\)/\1/g'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM