Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

mv myFile.txt myFile.txt.bak sed s/foo/bar/g myFile.txt.bak >myFile.txt or in on

ID: 3740564 • Letter: M

Question

mv myFile.txt myFile.txt.bak sed s/foo/bar/g myFile.txt.bak >myFile.txt or in one step by using the -i flag, which tells sed to edit the file in place" sed -1 s/too/bar/g myFile.txt In software development, however, substitutions like this often need to be made to many different fil developing a script to streamline the process. d of substitution is con 2 The Assignment You'll be working on this assignment in 3 stages. 2.1 Stage 1 1. Create a directory -/UnixCourse/seriptAsst. 2. Turn the two-line version, above, of the substitution commandsinto a shell script, subast 1l t taking parameters: o the string to be replaced o the string with which to replace it o the name of the file in which to make the substitution For example, echo "This is a test.">myFile.txt /UnixCourse/scriptAsst/substl is are myFile.txt cat myFile.txt should print the line Thare are a test. having replaced all occurrences of "is" in the file myrile. txt by "are", leaving the original file as Similarly echo "Heilo world.">aardvark.dat

Explanation / Answer

Create the directory and place a file named subst1.sh In that file place the following bash code. Change permissions by chmod a+x subst1.sh to make an executable.

sed -e 's/'$1'/'$2'/g' $3 > $3'.bak'

This uses the standard sed library to replace strings. -e option for not doing inplace as the old file shouldn't be changed. > redirects the output to new file named same as old file but appended with .bak. The middle portion is

's/old/new/g' old and new are strings which needs to be replaced any by whom respectively. $1 and $2 are inputs to executable.