[CentOS] Assitance with perl

H
Mon, 31 Jan 2022 01:12:48 -0000

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.

Thanks.