sed using wildcard

Hi Folks,

  I had a requirement to replace a pattern a.*a with 'a' alone. I'm writing a sed command to do that. But I'm not able to work this out. Pls help me.
echo 'a123a456a789' | sed 's/a.*a/a/'

Expected o/p : a456a789
But actual o/p is a789 . :confused:

how can write that search pattern in sed to replace the first occurance alone.?

Thanks,
Poova.

echo 'a123a456a789' | sed 's/a[^a]*a/a/'

And if that value is in a variable and you are using ksh93/bash (run shopt -s extglob in bash, first):

var=a123a456a789
echo ${var/a*([!a])a/a}
a456a789

Any shell:

var=a123a456a789

echo "a${var#a*a}"

With awk

echo 'a123a456a789' | awk '{ sub(/a[0-9]+a/,"a");print}'
a456a789

That will work in any shell but will give undesired results if shortest match for the pattern a*a is not found in the variable value.

This is true for any shell that supports the variable expansions specified by the POSIX standards and the Single UNIX Specifications for a variable that contains at least two "a" characters. The C shell ( csh ) and related shells usually do not support ${varOPpattern} expansions where OP is # , ## , % , or %% .