0

I have this code that makes substitutions in a file segment. The code should be left mostly unharmed except for this problem. I need to return back to bash,

  1. the number of replacements made by perl
  2. and the new string
  3. In an array of 2 elements (string, integer).

The problem I am facing is that if I wanted to return 2 numbers, or 2 words, then my code is fine.

But in this case, (with a multiline string in an array), when read in bash, it splits up into each constituent lines, and then it sticks the number as the last member of the array. Weird?

filesection='

   parameter=9
# parameter=10
parameter=10
'

            arrayRet=$( perl -le '
                    @lines=split(/\n/,$ARGV[0]); 
                    $C+=s/$ARGV[1]/$1$ARGV[2]/g for @lines; 
                    $S=join "\n", @lines; 
                    @array =($S,$C);
                    print @array;
                    ' "$filesection" ".*parameter.*" "replaced") 

                    #arrayRet=( "$arrayRet" )

echo "0=<${arrayRet[0]}> 1=<${arrayRet[1]}> 2=<${arrayRet[2]}> "    

Expected output:

0=<

replaced
replaced
replaced
    > 1=<3> 2=<>

PS. extra line breaks are intentional. extra echos are intentional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.