Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

CA114      CA170      CA686      CA686FL

Online AI coding exercises

Project ideas


Lab - cweb

We will get a script called "cweb" working. This script goes through your web pages, changing one string to another string.
Usage like:
cweb oldstring newstring



40%

Shell script setup:
  1. You need a bin directory.
  2. The bin directory must be in the path.
  3. This is probably already set up for you.
  4. You will have found this out when you did the Lab on shell.

  5. You need to make a script called "cweb" in the bin directory.
  6. It must be executable.


Web pages setup:

  1. You need some web pages to edit. You will copy these from me.
  2. Make a directory called "shakespeare" in your home directory.
  3. Copy my macbeth directory to your shakespeare directory.
    Q. How do you do this?

Demonstrate "sed" working:
  1. "cd" into your macbeth directory.
  2. Search for all occurrences of the string "Scotland":
    grep Scotland *html
    
  3. Change "Scotland" to "Finland" in the output stream:
    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.


100%

Get this script working.

 
if test "$2" = ""
then
 echo "Error. Not enough arguments."
 exit
fi

# Read in the arguments:

OLDSTRING=$1
(missing line)

cd $HOME/shakespeare/macbeth

for file in *html
do
 cat $file | sed -e "s|$OLDSTRING|$NEWSTRING|g" > tmpfile
 (missing line)
done


Q. What are the missing lines?


Testing

  1. "cd" into your macbeth directory.
  2. Search for all occurrences of the string "Scotland":
    grep Scotland *html
    
  3. Change all occurrences of "Scotland" to "Finland":
    cweb Scotland Finland
    
  4. Search for all occurrences of "Scotland".
  5. Search for all occurrences of "Finland".
  6. Change all occurrences of "Finland" to "Scotland".
  7. Search for all occurrences of "Scotland".
  8. Search for all occurrences of "Finland".




Notes




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).


ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.