Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain

Search:

CA114      CA170

CA668      CA669      Projects


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. You need to make a script called "cweb" in the bin directory.
  4. It must be executable.
  5. For all of this, see the Lab on shell.


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.

 
# Read in the arguments:

OLDSTRING=$1
NEWSTRING=$2

cd $HOME/shakespeare/macbeth

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


Q. What is the missing line?


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.