I have a CSV file with multiple before/after values which I am using to find and replace values in another large data file (~200MB).
I initially used a loop reading in each before/after value and sed to implement the find and replace.
The issue is that it's understandably a bit slow, so I wanted to try running all of the find/replace in a single line separated by semi-colons to see if it might be faster by only having to traverse the target data file one time.
So I have two values:
find="ABC"
replace="DEF"
Then I initialized the variable:
cmd=""
and within the loop, I tried to use this command:
cmd="${cmd}s/${find}/${replace}/g;"
The idea is to have everything concatenate into one long string like so:
"s/FIND1/REP1/g;s/FIND2/REP2/g;s/FIND3/REP3/g; ..." And so on
Then I could run the command:
perl -i -p -e ${cmd} TARGET_FILE
The issue is that my output for the cmd is looking really strange:
echo ${cmd}
/DEF/g;ABC
The order is totally messed up, I even tried to set ${cmd} to a string like "test" to see what was going on, and the output doesn't change. Somehow the variable order is getting reversed, and the leading "s" is not showing up.
I tried to use printf instead and got the same results. I tried removing the semicolon, changing the forward-slash, escaping the characters, and various other things but nothing seems to be working. Could someone tell me what is going on with this command and why the strange behavior?