Replace special characters in multiple files - perl

I have 100 files, where i want to search a set of strings and make the replacement by other strings

In the first case I want to include a parameter in the name of a file

LOG_DCT = $ LOG_DIR/DCT_GERAL_"$DATAINI".log
replace to : LOG_DCT = $ LOG_DIR / DCT_GERAL_ $ 1_ "$ DATAINI". log

I did tests with the instruction "perl -pi -e 's/DCT_GERAL/DCT_GERAL_ $ 1 /' filename�, but the result is not what we expected: LOG_DCT = $ LOG_DIR / DCT_GERAL__" $ DATAINI�.log replaces the $ 1 by null.

In the second case, I want to move the instruction of new line to the end of a printf

printf "\n# $IDCADEIA - $IDJOB - $HORAACTUAL - XPTO(YYYYMMDD)">> $ LOGFILE
Replace to: printf "$IDCADEIA - $IDJOB - $HORAACTUAL - XPTO(YYYYMMDD) \n#">> $ LOGFILE

I tried using the command "perl -pi -e 's/printf "\n#/printf "/' filename� and "perl -pi -e' s/) ">> $ LOGFILE /) \n#"> > $ LOGFILE/' filename�, but since the string has special characters ( ), \ ", #) it gives an error in the replacement.

Accepted suggestions of how to make these changes to files without having to have them manually: :smiley:

regards
Rui

The perl statement you are using requires a "g" at the end ... like so:

perl -pi -e 's/printf "\n#/printf "/g' filename

Also, remember that "\n" means New-Line in UNIX. So, you may have to escape it. To escape something you add a "\" in front to tell UNIX to take what comes next literally and not for its special meaning.

I always have to experiment a little when searching and replacing special characters to see what Perl, Ksh, Awk, Sed or whatever I'm using expects me to escape.

Cheers,
troym72