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 singledigit 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
containing thelines 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
-orgrep'CS4520' databook
-orgrep-w 'CS4520' databook
Note: Try CS4250
b. Print all lines where the person's first name starts with J.
ANSWER: grep'^[^ ]* J' databook
-oregrep "^[a-zA-Z0-9]* J" databook
c. Print all lines that contain .com
ANSWER: grep .com databook
-orgrep‘.com’ databook
-oregrep\.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
-orsed1,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
-orsed'/CS2010/c\NULL NULL NULL NULL' databook
Question 4: (15) (awk exercise)
Use the file 'databook1' as database
( The filecontains the lines of the following format:
Type first-name last-name email-address
Do the following queries:
a. Print all email address.
ANSWER: awk '{print $4}' databook1
b. Print Dumitru's name and email address.
ANSWER: awk '/Dumitru/ {print $2,$3,$4}' databook1
c. Change the type with the following rule:
If line number modulo 3 is 1, the type field should be changed to
CS4520. If line number modulo 3 is 2, the type field should be changed to
CS2210. Otherwise keep the original type field.
ANSWER: $ cat q4c
NR%3==1 {print "CS4520 " $2 " " $3 " " $4}
NR%3==2 {print "CS2210 " $2 " " $3 " " $4}
NR%3==0 {print $1 " " $2 " " $3 " " $4}
^D
$ awk -f q4c databook1
-orawk'NR%3 == 1 {$1 = "CS4520"} NR%3 == 2 {$1 = "CS2210"} {print $0}' datebook1
-or-
$ cat q4c.2
{
if (NR%3==1) {print "CS4520 " $2 " " $3 " " $4;}
else if (NR%3==2) {print "CS2210 " $2 " " $3 " " $4;}
else if (NR%3==0) {print $1 " " $2 " " $3 " " $4;}
}
^D
$ awk -f q4c.2 databook1
Question 5: (10) (Exercise for transforming files)
Write a command that archives and compresses the contents of afile, and writes the result to a new file. What is the correspondingcommand to extract it to produce the original file?
Answer: tar -zcvf file.tar filename;
tar -zxvf file.tar
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