344g
CSCI 344
Sample Sed Problems


How Assignments Work

Let me know when you have solved the problems below.  I will come to your terminal and you can demonstrate that you have the solution.  You will get full credit once you demonstrate all the answers.

You must show me your answers in lab.  If you solve the problems on your labtop during lecture I will look at them in lab.  I will not look at them in class.

If you can't finish an assignment, complete it out of lab and show me during the next lab.

If you don't show me your answers the day assigned or during the next lab you will receive a zero.

You may search the web for reference material, but it is considered cheating to search for a specific answer.

You may discuss the problems with classmates.  You may give hints, clues, and helpful advice to each other, but don't give each other the answers.


Review & Resources

Basic pattern matching metacharacters


.

Match any character

*

Match zero or more of the preceding characters (different than shell!!!)

^

Match beginning of line (or string)

$

Match end of line (or string)

\

Escape the following character

[]

Match one from set inside of [] (ranges, ^ for not)

\( ... \)

Store pattern for later use

\n

Replay sub-pattern in match (from \( \) )


Gnu sed Reference Manual



Problems

Write a sed script to do each of the following:
    1. Delete leading whitespace (all spaces and tabs) from the front of each line in a file
    2. Delete trailing whitespace from end of each line
    3. Insert 5 blank spaces at the beginning of each line
    4. Substitute "foo" with "bar" ONLY on lines which contain "baz"
    5. Substitute "foo" with "bar" EXCEPT for lines which contain "baz"
    6. Change "scarlet" or "ruby" or "puce" to "red"
    7. Print first 5 lines of file
    8. Print only line which match a regular expression
    9. Delete the first 10 lines of a file