Uploaded by skado

Lab exercise 6 140322

advertisement
Lab exercise 6 - Using grep command and Regular Expressions (18 marks)
Date: 14th March, 2022
grep searches the input files for lines containing a match to a given pattern list.
When it finds a match in a line, it copies the line to standard output (by default), or
whatever other sort of output you have requested with options.
Part 1
/etc. This is the nerve center of the Linux system, it contains all system related
configuration files. A "configuration file" is defined as a local file used to control the
operation of a program; it must be static and cannot be an executable binary.
The /etc/passwd file is a colon-separated file that contains the following information:







User name.
Encrypted password.
User ID number (UID)
User's group ID number (GID)
Full name of the user (GECOS)
User home directory.
Login shell.
Work to do:
i.
Working from the home directory, use grep command to display the lines from
/etc/passwd containing the string root. (2 marks)
ii.
Use grep to display the line numbers containing the search string in (i) above.
(2 marks)
iii.
Use grep to check which users are not using bash (2 marks)
iv.
Utilize grep to counts the number of accounts that have /bin/false as the shell.
(2 marks)
v.
Use grep command to display the lines from all the files in your home
directory starting with ~/.bash, excluding matches containing the string history,
so as to exclude matches from ~/.bash history which might contain the same
string, in upper or lower cases. (2 marks)
Part 2
grep, using regular expressions
i. Still working with the previous directory path (/etc/passwd), exclusively display
lines starting with the string "root" (2 marks)
ii. Display which accounts have no shell assigned whatsoever, hint: we search
for lines ending in ":" (2 marks)
iii. A bracket expression is a list of characters enclosed by "[" and "]". It matches
any single character in that list; if the first character of the list is the caret,
"^", then it matches any character NOT in the list.
For example, the regular expression "[0123456789]" matches any single digit.
Use grep to display all the lines containing either a "y" or "f" character in
the /etc/group directory path (2 marks)
iv. Access the system’s English dictionary to search for words.
kndlovu@LAB120R6C1:~$ cd /usr/share/dict
grep ‘\<word\>’ /usr/share/dict/words (0 mark)
v. Use the "." for a single character match to get a list of all five-character
English dictionary words starting with "c" and ending in "h" (handy for
solving crosswords) (2 marks)
vi. For matching multiple characters, use the asterisk (*). Use grep to select all
words starting with "c" and ending in "h" from the system's dictionary (0
mark)
vii. If you want to find the literal asterisk character in a file or output, use single
quotes. In the example below the asterisk character in /etc/profile is used
without using quotes, which does not return any lines. Using quotes,
output is generated: (0 mark)
TRY the following lines one at a time:
~> grep * /etc/profile
~> grep ‘*’ /etc/profile
*******************END OF LAB EXERCISE***********************
Download