[CentOS] Assitance with perl

Thu Feb 3 06:14:38 UTC 2022
David Christensen <dpchrist at holgerdanske.com>

On 1/30/22 17: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.
> 
> Thanks.


On 2/2/22 17:54, H wrote:
 > I am still having a problem. The following (where $txt is an 
arbitrary string) works:
 >
 > perl -e 'print '"\"${txt}\""';'
 >
 > The following does not work (I want to append the content of the $txt 
to the end of an existing file in-place):
 >
 > perl -i -pe 'eof && do{print $_''\"aaa\"''; exit}' somefile.txt
 >
 > but this does:
 >
 > perl -i -pe "eof && do{print $_""\"${txt}\""'; exit}' somefile.txt
 >
 > as does:
 >
 > perl -i -pe "eof && do{print $_""\"${txt}\"""; exit}" somefile.txt
 >
 > The difference is that the last two perl command strings use " rather 
than '.
 >
 > My questions are:
 >
 > - Why would not using single-quotes for parts of the perl command 
string work?
 >
 > - Is there any reason I should fight this or should I just go with 
double-quotes for all parts of the perl command string? Any downside? 
Remember, these are all in bash scripts and I am looking for a "pattern" 
to use for other, more complicated text substitutions, hence the use of 
perl.


On 2/2/22 18:55, H wrote:
 > I see I made a mistake, the line:
 >
 > perl -i -pe 'eof && do{print $_''\"aaa\"''; exit}' somefile.txt
 >
 > should be:
 >
 > perl -i -pe 'eof && do{print $_''\"${txt}\"''; exit}' somefile.txt
 >
 > Related question, if the $txt string contains eg $ or another special 
character, what would be the best way of escaping it so it is not 
substituted by perl?


AIUI you are looking for a Bash shell scripting (programming) technique 
that allows you to append content to a file using a Perl one-liner with 
data that is dynamically generated from Bash variable values, all inside 
a Bash script (?).


Perhaps it would be simpler if you put the data into a file and then 
invoked the Perl one-liner with the data file filename as an argument:

2022-02-02 22:08:34 dpchrist at tinkywinky ~
$ cat somefile.txt
this is the contents of somefile.
some more contents.

2022-02-02 22:08:37 dpchrist at tinkywinky ~
$ cat centos-h.sh
#!/bin/bash
txt="... foo ..."
echo "$txt" > tmp.txt
perl -pe 's/foo/bar/g' tmp.txt >> somefile.txt

2022-02-02 22:08:46 dpchrist at tinkywinky ~
$ bash centos-h.sh

2022-02-02 22:08:53 dpchrist at tinkywinky ~
$ cat somefile.txt
this is the contents of somefile.
some more contents.
... bar ...


David