Recent Changes - Search:
 Welcome to the Cisco Academy for Vision Impaired Linux Wiki.

PmWiki

edit SideBar

RegularExpressionExercise1

Regular expression exercise

This exercise is due in a week from when it is received.

Regular expressions are a handy skill to have not only under Linux but under other operating systems when data needs to be searched and manipulated. There is an excellent website on regular expressions at
http://www.regexp.info
which will help you with the following exercises.

We'll need some text in a file to play with. You can put this in with cat, nano ed or whatever. Careful cutting and pasting if you go this route you may get unintended characters in your file. If you call it party.txt winscp will do the conversions of line endings for you or you can use flip -u once the flip package is installed.

Here is the text:

Now is the time
for all good men
to come to the aid of their party.

a frog and a dog sat on a log.
Dorian Gray was having a grey day.

You should use ed or sed to run the following exercises, grep can be used for the read-only tests.

Give the patterns to achieve the following:

  • What expression will search for the line containing party?
  • How many lines match?
  • How could we change party to birthday party? what does & (ampersand) do in a regular expression and how can we use it?
  • which substitute expression in ed or sed will change men to women?
  • what expression could we use to change women to men and women using ampersand?
  • If you search for og how many lines match?
  • what line do you get when you do s/og/ag/
  • Now put the line back to how it was.
  • what expression will change frog log and dog to frag lag and dag?
  • How to put it back again with one substitute?
  • Why wont the following fix the previous line?

s/.ag/.og/g

  • How would we match log and dog but not frog?
  • If the line contained drag and frog how could we match these without matching log and dog?
  • How could you match log or dog regardless of the case of the first letter? Do not use the -i option here.
  • How could we match gray or grey?
  • In a regular expression what is the difference between *.* and .*?
  • Why does echo *

show hidden files in your home directory like .bash_profile?

  • How would I find files that start with letters a through e?
  • How about any file that starts with an uppercase letter?
  • Hannah is a typist for the Daily Bore. Unfortunately her shift finger is sticky and she ends up writing copy like this:

TOday the premier stated that he would not stand for the changes to the act. HE said that if JErry wanted to change the system he should go through due process.

People have complainned about Hannah's articles and she is at risk of loosing her job. Can you design an expression to catch more than one uppercase letter followed by lowercase ones?

  • What is an expression that will find heading tags in html such as <h1>
  • can you make the expression find <h1> and <h1 >
  • How will you stop it matching n1h1?
Edit - History - Print - Recent Changes - Search
Page last modified on March 21, 2013, at 12:22 AM