I recently wrote a script that does a sed command, to replace all the occurrences of “string1” with “string2” in a file named “test.txt”.
It looks like this:
sed -i 's/string1/string2/g' test.txt
The catch is, “string1” does not necessarily exist in test.txt.
I notice after executing a bunch of these sed commands, I get a number of empty files, left behind in the directory, with names that look like this:
“sed4l4DpD”
Does anyone know why this might be, and how I can correct it?
So after much testing last night, it turns out that sed was creating these files when trying to operate on an empty string. The way i was getting the array of “$string1” arguments was through a grep command, which seems to be malformed. What I wanted from the grep was all lines containing something of the type “Text here ‘.'”.
For example the string, “
Text here 'ABC.DEF'” in a file, should have been caught by grep, then theABC.DEFportion of the string, would be substituted byABC_DEF. Unfortunately the grep I was using would catch lines of the type “Text here ''” (that is, nothing between the ”). When later on, the script attempted to perform a sed replacement using this empty string, the random file was created (probably because sed died).Thanks for all your help in understanding how sed works.