Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What are regular expressions? What are they used for? (each 10 points) 2. Ass

ID: 3547498 • Letter: 1

Question

1. What are regular expressions? What are they used for? (each 10 points)

2. Assume you have a file that contains these lines:

a3b

adcbcbc

12c5a36

a5

3c

1) In the above file which line(s) will match the regular expression of '[a3][5c]' ? (10 points)

2) In the above file which lines will the regular expression of '^.3.*b' match? (10 points)

3. Explain what this regular expression will match: (10 points)

   abcd{5}

4. Write a grep command that get a list of files that contain the string zip preceded by any character except b or g. (10 points)

5. Given a file containing the lines at the right, list which lines will be matched by each of the following egrep patterns: (each 5 points)

cat
catch
cart
crepe
oxcart
encarta
fire
fireplace
family
fame

1. '^c'

2. 'e$'

3. '^....$'

4. 'c.r'

5. 't.$'

6. 't.?$'

7. 't.+$'

8. 't.*$'

9. '^.[ai]'

10. '^.[^ai]'


1. What does the following awk command do and output? (20 points)

     awk  

Explanation / Answer

1. Set of chars that repeats itself in the form of pattern

2a. a5, 3c [grep '[a3][5c]' file]

2b. a3b [grep '^.3.*b']

3. It searches for abcd{5}. [here is escape char]

4. ls -f1|grep '.*[^b^g].zip'

5.1. cat catch cart crepe - [begins with c]

5.2. creppe fire fireplace fame - [ends with e]

5.3. cat - [contains only 3 char]

5.4. cart oxcart encarta - [words containing 3 alphabets with c as start and t as end]

5.5. encarta - [ends with t and some char]

5.6. cat cart oxcart encarta - [ends with t or t with one more char]

5.7. catch encarta - [t with any no of chars after t]

5.8. cat catch cart oxcart encarta [ends with t or t followed by any no of char]

5.9. fire fireplace family fame - [second char is a or i]

5.10. cat catch cart crepe oxcart encarta - [second char is not a or i]



1. Prints the number of lines which are null [or] "".

2. sed -e 's/.{80}/&;/g' prg

3. sed -n '1,10p' file

4. grep '^[^.]+::' /etc/passwd | awk 'BEGIN {print $1}'

5. awk '{print NR " " $1}' file

6. sed -e 's/^#//g' prg