On 01/31/2022 09:59 PM, H wrote: > 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... > 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. Thank you! _______________________________________________ CentOS mailing list CentOS@xxxxxxxxxx https://lists.centos.org/mailman/listinfo/centos