Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

Free AI exercises


Einstein - cweb

  
"Change web pages" Shell script.
This script goes through your web pages, changing one string to another string.
Usage like:
cweb oldstring newstring *html

  

Einstein test environment

As already explained, in the special Einstein test environment, we need extension .sh and need to call it with "./" in front of it.
(In a normal environment, neither would be needed.)
Also for technical reasons in this test we will call it cweb3.
So the conclusion is that, for this test, we need a file called cweb3.sh and we run it like this:
./cweb3.sh oldstring newstring *html


Debug with Shakespeare files

  1. You need some web pages to change.
  2. Download your own copy of the Shakespeare files.

See how "sed" works

Before even writing the program, first see how "sed" works:
  1. "cd" into the "macbeth" directory in your own copy of the Shakespeare files.
  2. Search for all occurrences of the string "Scotland":
     grep Scotland *html 
  3. Change "Scotland" to "Finland" in the output stream by piping the output of grep into sed:
     grep Scotland *html | sed -e "s|Scotland|Finland|g" 
  4. This changes the output stream on the command line. It does not change the files.


Modify this script

Here is a script to get started.
Anything in [red brackets] has to be fixed by you. You can literally find all of these in my notes. Use the search engine on my site to search my notes.

# Read in the arguments:
OLDSTRING=[first argument]
NEWSTRING=[second argument]

# the following gets rid of the first two arguments
shift
shift

# from now on, "all arguments" means "all arguments from 3rd one on"
# go through all arguments one by one:

for file in [all arguments]
do
 echo "We need to change $OLDSTRING to $NEWSTRING in $file" 
 ls -l $file 
 echo 
done

To test the above, "cd" into your macbeth directory.

In the special Einstein test environment, we move the program into the same directory as the files. But normally this would not be needed.

Test it with:

./cweb3.sh Scotland Finland complete.html
./cweb3.sh Scotland Finland *html
It should just do an "ls" of the files in question, and say what string needs to be changed to what. But not actually change anything.

Test the above locally. Do not move on until this is working.




How to actually change the files

  1. At the point where we do "ls" above, print out the contents of the file with "cat".
  2. Send that via a pipe to "sed" to change one string to another (as above).
  3. Note that "sed" should change the old string to the new string, whatever they are. It should not be hardwired to change Scotland to Finland.
  4. Redirect the output to a temporary file.
  5. So something like this:

     [cat command] | [sed command] > [temporary file]
    

  6. Use file commands to copy the temporary file back to the original file. Now the program works!
  7. Finally, I want you to "ls" the file (long version) before and after the change, so we can see the file size changing.

Test the above locally and make sure it works. Do not just try to upload.




Test and upload

  1. In the macbeth directory, search for occurrences of the string "Scotland" in one file:
    grep Scotland complete.html
    
  2. Change all occurrences of "Scotland" to "Finland":
    ./cweb3.sh Scotland Finland complete.html
    
  3. Search for occurrences of "Scotland".
  4. Search for occurrences of "Finland".
  5. Change all occurrences of "Finland" back to "Scotland".
  6. Search for occurrences of "Scotland".
  7. Search for occurrences of "Finland".

  8. When that is working, test with multiple files:
    ./cweb3.sh Scotland Finland *html
    
  9. When that is working, upload to Einstein for marks.



This script is very useful

Imagine using this script to change one string to another in 1,000 web pages without having to open any editors (or indeed do any work).