Re: sed output and test for nonzero string

Brendan wrote:
Hi there, The following does not work,

Please specify what exactly "does not work" in your case means. Provide
input data and output or error responses.

I assume because sed tacks a
newline on and $t is never a zero length string:

The $( ... ) removes any trailling whitespace characters.

Are you maybe working on a WinDOS file and have some spurious CR at the end?

t=$(echo $f | sed -n -r '/co[0-9]{1,2}@[a-z]/p')
echo $t
if [ -n $t ]; then
f=$(echo $f | sed 's/@/@_/')
echo $f

How do I correctly make this test? Insert an underscore after @ if one
does not already exist, only for strings matching the first pattern?

Provide examples of input data and corresponding output data then it's
easier to show possible solutions. Generally testing with [ -z "$var" ]
or [ -n "$var" ] (or [[ -z $var ]] and [[ -n $var ]] with modern shells
shall work.