script for this assignment is find_and_replace.sh. You will need to use sed to r
ID: 3667347 • Letter: S
Question
script for this assignment is find_and_replace.sh. You will need to use sed to replace all instances of one string in a file. Specifically, find_and_replace.sh should take 4 arguments. The first is the file to read from, the second is the file to output to, the third is the string to find, and the fourth is the replacement string. The script itself is relatively simple. The script must read every line from the input file no matter how many lines there are. It then replaces all instances of the first string given with the second string. Finally, it outputs every line to the output file whether or not any substitutions happened.
As always you need to do error checking. Specifically, check the number of arguments and that the first argument is actually a file. Note that you don't need to check if the second argument exists or because your script should output to that file either way. In addition, you'll need a way to send the input file in to a loop to read every line of that file. You can use I/O redirection with the entire loop. The same idea will be used to send the output of the loop to the output file. Here is some example output:
[user@localhost lab3]$ ./find_and_replace.sh
usage: find_and_replace.sh existing_file new_file current_string new_string
[user@localhost lab3]$ cat jedi_code
Jedi code:
There is no emotion, there is peace.
There is no ignorance, there is knowledge.
There is no passion, there is serenity.
There is no chaos, there is harmony.
There is no death, there is the Force.
[user@localhost lab3]$ ./find_and_replace.sh jedi_code jedi.out "is no" "ain't"
[user@localhost lab3]$ cat jedi.out
Jedi code:
There ain't emotion, there is peace.
There ain't ignorance, there is knowledge.
There ain't passion, there is serenity.
There ain't chaos, there is harmony.
There ain't death, there is the Force.
[user@localhost lab3]$
Once you've got it tested and working, TAKE SCREENSHOT 2 of the output of the script for a sample run. Only provide a single screenshot. More than one will be ignored. PLACE SCREENSHOT 2 HERE:
Explanation / Answer
Run the below code in your system. It will do the needful.
sed -i -e 's/TEXT TO BE REPLACED/REPLACED TEXT/g' Newfile.sh
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.