Hi all,
I am trying to write a script substituting one word in a particular file with another word (sed) but I'm having trouble creating the backup file. The following are my instructions:
The Unix program sed is useful for making simple substitutions throughout an entire file. But one of its limitations is that it always puts its results onto standard output and therefore it cannot be used to edit a file "in place". For example, the command
sed s/foo/bar/g myFile.txt
will replace every occurrence in myFile.txt of the string "foo" by the string "bar", with the altered file appearing at the standard output (i.e., scrolling by on your terminal screen). But what if you really wanted to make this change
to myFile.txt? You can't do this:
sed s/foo/bar/g myFile.txt > myFile.txt
because the output redirection begins by creating an empty output file myFile.txt, thus destroying the input file before we get achance to read it. Instead, we wind up doing this in two or more steps: mv myFile.txt myFile.txt.bak
sed s/foo/bar/g myFile.txt.bak > myFile.txt
In software development, however, substitutions like this often need to be made to many different files. For example, a decision to change a variable name may affect several different source code files. This kind of substitution is common enough that it's worth developing a script to streamline the process.
- Create a directory ~/UnixCourse/scriptAsst.
- Turn the two-line version, above, of the substitution commands into a shell script, subst1 taking three parameters:
- the string to be replaced
- the string with which to replace it
- the name of the file in which to make the substitution
For example,
~/UnixCourse/scriptAsst/subst1 foo bar myFile.txt
should replace all occurrences of " foo" in the file myFile.txt by " bar", leaving the original file as myFile.txt.bak.
Similarly,
~/UnixCourse/scriptAsst/subst1 abc "" aardvark.dat
should remove (replace by the empty string) all occurrences of " abc" in the file aardvark.dat by, leaving the original file as aardvark.dat.bak.
- One problem with this approach is that the file's creation and modification dates are altered, even if no change was made to the file contents (because the string being relaced did not occur within the file). Create a new script, subst2, taking the same three parameters, that performs the substitution and backup file creation when the target string occurs somewhere in the file, but leaves the file completely unchanged (and does not create the .bak file) if the string being replaced is nowhere in the file. (Hint: there are two ways to do this. One is to check first for the presence of the target string and, if it is found, to do the two-step substitution as above. On average, though, it may be faster to go ahead and do the substitution, and then to check and see if the resulting files are identical.)
- Generalize your subst2 script, producing a new script subst that will apply a substitution to any number of files given on the command line. For example ~/UnixCourse/scriptAsst/subst foo bar myFile1.txt myFile2.txt
myFile3.txt
should apply the same substitution throughout each of the three files named there.
I am having a very difficult time with this assignment. Here is what I have for subst1:
#!/bin/sh
find=$1
replace=$2
test=$3
mv $test $test.txt.bak
sed s/$find/$replace/g $test.txt.bak > $test
Also, I am unsure how to do part 3 and part 4. I was wondering if part 4 consisted of using the following in order to apply the substitution to any number of files given on the command line:
#!/bin/sh
for x in $*
do
echo $x
done
Am I on the right track? Any help at all will be greatly appreciated. I am so stressed about this problem!