Post On Bash Scripts Text Replacement

I needed to replace text in several files so I wrote a Bash script to do it.
There are several ways. One of the simplest is using the built-in search-and-replace function in Bash bracket expansion.

read -p "Enter the common filename without extension: " orignam
read -p "Enter the new name: " newname

while IFS='' read -r a; do
    echo "${a//$orignam/$newname}"
done < $orignam.html > $newname.html

rm $orignam.html

Brief Description

The user can fire off the script. Type in the main part of the file name, it is stored. Type in the main part of the new name, it is stored. Using ‘<’ redirection, the file is fed in, the script looks at the text line by line using ‘read’ with a ‘while’ loop, using ‘echo’ if the text pattern is matched it is replaced then the results are output into a new file. The original file is deleted.

This is used to change the file references in HTML
Content of input file:
<script src="19rect-xp02.js"></script>
Content of output file:
<script src="20rect-xp02.js"></script>

read

Manual page for ‘read’
https://ss64.com/bash/read.html

Bracket Expansion

In Bash dollar sign and curly brackets ${}
Some exhaustive documentation is in GNU.org
https://www.gnu.org/s/bash/manual/html_node/Shell-Parameter-Expansion.html
More here
https://tldp.org/LDP/abs/html/parameter-substitution.html

IFS

Stretch Goals

If I wanted more specific text selectors I could use more sophisticated Regular Expressions.
An online tool to generate or interpret regular expressions
https://regexr.com
There are a lot of resources but the earliest or “purest” source was the man page for ‘grep’
https://linux.die.net/man/1/egrep

If I wanted to run the script on all files in a folder I could use a glob “*” or positional parameters. That way I could drag files right into the Terminal and Terminal will autocomplete the full path.

General shell script resources
http://linuxcommand.org/index.php