Docsity

Regular Expressions, Grep, Sed and Awk Exercises with Examples - Prof. Chunyu Ai, Assignments of Computer Science

Toggle sidebar
bg1
Pratice 5
Question 1 (15)
Describe the meaning for each regular expression below. For each
expression, give 3 different examples that match the pattern.
a) /^[A-Z]..$/
ANSWER: A single uppercase letter at the beginning of a line followed
by two of any kind of character and the end of the line.
Aa1
B!A
X12
b) /^[A-Z][a-z]*3[0-5]/
ANSWER: At the beginning of a line, an uppercase letter, followed by
zero or more lowercase letters, and followed by the digit 3, then a
single digit between 0 and 5.
Aabcdef35
B31
Xabcdefghijklmnopqrstuv30
c) /^[A-Za-z]*[^,][A-Za-z]*$/
ANSWER: At the beginning of a line, zero or more letters followed by a
character other than a comma, followed by zero or more letters and the
end of the line.
AaBbCc7AaBbCc
a+b
xXx.
Question 2: (20) (Grep/egrep exercise)
Given the file 'databook' (you can download it from website
http://www.cs.gsu.edu/~chunyuai/3320/hw/databook) containing the lines of the
following format:
Type first-name last-name email-address
Do the following queries:
a. Print all lines containing the string CS4520.
ANSWER: grep CS4520 databook
-or grep 'CS4520' databook
pf3
pf4

Partial preview of the text

Download Regular Expressions, Grep, Sed and Awk Exercises with Examples - Prof. Chunyu Ai and more Assignments Computer Science in PDF only on Docsity!

Pratice 5

Question 1 (15) Describe the meaning for each regular expression below. For each expression, give 3 different examples that match the pattern. a) /^[A-Z]..$/ ANSWER: A single uppercase letter at the beginning of a line followed by two of any kind of character and the end of the line. Aa B!A X b) /^[A-Z][a-z]3[0-5]/ ANSWER: At the beginning of a line, an uppercase letter, followed by zero or more lowercase letters, and followed by the digit 3, then a single digit between 0 and 5. Aabcdef B Xabcdefghijklmnopqrstuv c) /^[A-Za-z][^,][A-Za-z]*$/ ANSWER: At the beginning of a line, zero or more letters followed by a character other than a comma, followed by zero or more letters and the end of the line. AaBbCc7AaBbCc a+b xXx. Question 2: (20) (Grep/egrep exercise) Given the file 'databook' (you can download it from website http://www.cs.gsu.edu/~chunyuai/3320/hw/databook) containing the lines of the following format: Type first-name last-name email-address Do the following queries: a. Print all lines containing the string CS4520. ANSWER: grep CS4520 databook -or grep 'CS4520' databook

-or grep -w 'CS4520' databook Note: Try CS b. Print all lines where the person's first name starts with J. ANSWER: grep '^[^ ]* J' databook -or egrep "^[a-zA-Z0-9]* J" databook c. Print all lines that contain .com ANSWER: grep .com databook -or grep ‘.com’ databook -or egrep .com databook d. Print all lines containing an upper-case letter, followed by four lower-case letters, a space and one uppercase letter. ANSWER: egrep "[A-Z][a-z]{4} [A-Z]" databook Question 3: (20) (sed exercise) Use the file 'databook', do the following queries: a. Change Dumitru's name to Dima. ANSWER: sed 's/Dumitru/Dima/' databook > db1.txt b. Delete the first 3 line. ANSWER: sed '1,3 d' databook> db2.txt -or sed 1,3d databook > db2.txt c. Print all lines where the email address contains @student.gsu.edu ANSWER: sed -n 's/@student.gsu.edu/&/p' databook d. Replace the line containing "CS2010" with "NULL NULL NULL NULL". ANSWER: sed 's/^.CS2010.$/NULL NULL NULL NULL/' databook > db3.txt -or sed '/CS2010/c\NULL NULL NULL NULL' databook Question 4: (15) (awk exercise) Use the file 'databook1' as database (http://www.cs.gsu.edu/~chunyuai/3320/hw/databook1). The file contains the lines of the following format: Type first-name last-name email-address Do the following queries:

Question 6: (10) (Exercise for searching files) Write a command to find files in a directory hierarchy (e.g. your home directory) that have the name pattern '.txt" and compress them. find ~ -name '.txt' -ls -exec tar -zcvf txt.tar {} ; Question 7: (10) (Exercise for sorting files) Write a command pipeline to list files in a directory hierarchy (e.g. your home directory) that have the name pattern '*.txt" and sort them by file size. ANSWER: ls -al *.txt | sort +4 -5 -n