Question: Guide to UNIX Using Linux (4th Edition) 1. What is grep? The grep command searches the input files for lines containing a match to a
Guide to UNIX Using Linux (4th Edition)
1.What is grep? The grep command 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.
Though grep expects to do the matching on text, it has no limits on input line length other than available memory, and it can match arbitrary characters within a line. If the final byte of an input file is not a newline, grep silently supplies one. Since newline is also a separator for the list of patterns, there is no way to match newline characters in a text. Given the following commands in a. through e. below, explain what each does:
a. /home/cathy01> grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
b. /home/cathy01> grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin
c. /home/cathy01> grep -v bash /etc/passwd | grep -v nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
news:x:9:13:news:/var/spool/news:
mailnull:x:47:47::/var/spool/mqueue:/dev/null
xfs:x:43:43:X Font Server:/etc/X11/fs:/bin/false
rpc:x:32:32:Portmapper RPC user:/:/bin/false
nscd:x:28:28:NSCD Daemon:/:/bin/false
named:x:25:25:Named:/var/named:/bin/false
squid:x:23:23::/var/spool/squid:/dev/null
ldap:x:55:55:LDAP User:/var/lib/ldap:/bin/false
apache:x:48:48:Apache:/var/www:/bin/false
d. /home/cathy01> grep -c false /etc/passwd
e. /home/cathy01> grep -i ps ~/.bash* | grep -v history
/home/cathy01/.bashrc:PS1=\[\033[1;44m\]$USER is in \w\[\033[0m\]
a. _____________________________________________________________________
b. _____________________________________________________________________
c. _____________________________________________________________________
d. _____________________________________________________________________
e. _____________________________________________________________________
From the previous examples in /etc/passwd, show the command to now exclusively display lines starting with the string root: f. _______________________________________________________________________ From the previous examples in /etc/passwd, show the command to see which accounts have no shell assigned whatsoever (i.e., search for lines ending in :) g. _______________________________________________________________________ Show the command to check that PATH is exported in ~/.bashrc by first selecting export lines and then searching for lines starting with the string PATH so as not to display MANPATH and other possible paths: h. _______________________________________________________________________
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.
Within a bracket expression, a range expression consists of 2 characters separated by a hyphen. It matches any single character that sorts between the 2 characters, inclusive, using the locales collating sequence and character set. For example, in the default C locale, [a-d] is equivalent to [abcd]. Many locales sort characters in dictionary order, and in these locales [a-d] is typically not equivalent to [abcd]; it might be equivalent to [AaBbCcDd], for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value C. Finally, certain named classes of characters are predefined within bracket expressions. Given the following command, explain what it does:
/home/cathy01> grep [yf] /etc/group
sys:x:3:root,bin,adm
tty:x:5:
mail:x:12:mail,postfix
ftp:x:50:
nobody:x:99:
floppy:x:19:
xfs:x:43:
nfsnobody:x:65534:
postfix:x:89:
i. _____________________________________________________________________ Use the . for a single character match. Say you want to get a list of all 5-character English dictionary words starting with c and ending in h (handy for solving crosswords) from the file /usr/share/dict/words provide the command to do so below using grep: j. _______________________________________________________________________ If you want to display lines containing the literal dot character, use the _____ option to grep.
For matching multiple characters, use the asterisk (*). Show a command that selects all words starting with c and ending in h from the system's dictionary at /usr/share/dict/words : k. _______________________________________________________________________ If you want to find the literal asterisk character(s) in a file or output, use single quotes. Provide the grep command to find the asterisk character(s) in /etc/profile : l. ________________________________________________________________________
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
