[CentOS] Assitance with perl

H
Tue, 01 Feb 2022 02:59:46 -0000

On 01/30/2022 11:00 PM, Orion Poplawski wrote:
> On 1/30/22 18:12, H wrote:
>> I am writing a long bash script under CentOS 7 where perl is used for
manipulating some external files. So far I am using perl one-liners to do so but
ran into a problem when I need to append text to an external file.
>>
>> Here is a simplified example in the bash script where txt is a bash
variable which I built containing a longish text with multiple newlines:
>>
>> txt="a b$'\n'cd ef$'\n'g h$'\n'ij kl"
>>
>> A simplified perl one-liner to append the text in the variable above to
some file in the bash script would be:
>>
>> perl -pe 'eof && do{print $_'"${txt}"';
exit}' someexternalfile.txt
>>
>> This works when fine when $txt does /not/ contain any spaces but falls
apart when it does.
>>
>> I would like to keep the above structure, ie using bash variables to
build text strings and one-liners to do the text manipulation. Hopefully there
is a "simple" solution to do this, I have tried many variations and
failed miserably... Note that I also want to use a similar pattern to do
substitutions in external files, I would thus like to use the same code pattern.
>
> I don't understand why:
>
> echo -e $txt >> someexternalfile.txt
>
> doesn't do what you want, or if perl is absolutely what you need:
>
> perl -e "print \"${txt}\";" >>
someexternalfile.txt
>
> I have no idea if you are trying to output literal $'s or 's or
not.
>
Thank you, it works! I had forgotten to escape the quotes around my bash
variable...