-4

I'm new to linux. I want to be placed as a System Admin. I attended an interview at Amazon. They asked me to write a cmd to find the string that starts with 's' and ends with 'a' in a file. I jz know that we have to use grep, bt don't know the actual things. Added to this they also asked how grep works. Can anyone brief out an answer for this.

Thanks in advance

user72789
  • 123
  • 8
    Um, no offense, but if you don't know the answers to these questions, you're several years away from being able to land a job as a sysadmin. – Tom Zych Jun 17 '14 at 12:35
  • 2
    Also, don't double-post - i.e. don't post the same question as multiple sites. If it had been a good enough question for [unix.se], we at ServerFault would have migrated it here. – Jenny D Jun 17 '14 at 12:44
  • 2
    I suggest you read through man grep, everything you need to know is there. – terdon Jun 17 '14 at 13:26

2 Answers2

8

Try this GNU grep command with -P(Perl-regex) option,

 grep -oP '\bs.*a\b' file

Explanation:

\b    # Matches the word boundary(ie, match between a word character and a non word character)
s     # Starting character would be a literal s.
.*    # Matches any character zero or more times.
a     # Matches a literal a.
\b    # Matches the word boundary(ie, match between a word character and a non word character)
Avinash Raj
  • 3,703
8

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines

Grep Commands

Grep and its Options

-v  =   non-matching lines
-c  =   count of matching  lines
-i  =   Ignore  case
-r  =   Read all  files  under  each  directory
-l  =   list the file which contain the searching keyword
 ^      =       Search the Starting word of a file
.$      =       Search the end word of a file
^$      =       Search the empty lines in a file

Find all lines matching a specific keyword on a file.

grep sysadmin /etc/passwd

Display with numbers

grep -nv nologin /etc/passwd

To avoid the keyword and search for other

grep -v sysadmin /etc/passwd

To count how many lines match for the keywords we are searching

grep -c sysadmin /etc/passwd

To search text by ignoring the case

grep -i sysadmin /etc/passwd

Search all files in /home and its subdirectories for text matching a specific pattern and print the matching lines

grep -ri sysadmin /home/

Search all files in /home and its subdirectories for text matching a specific pattern and list the files which contain it

grep -ril sysadmin /home/

To search for a particular IP mentioned among many IPs. The -F ensures that the . match literal . characters, without it they would match any character.

grep -F "192.168.1.10" /var/log/syslog

To search for a line that starts with Feb

grep ^Feb /var/log/syslog

To search for a line that ends with queue

grep queue$ /var/log/mail.log

To count the empty lines in a file (lines containing whitespace will not be counted)

grep -c ^$ /var/log/mail.log

Search for a string inside all files of a directory and its subdirectories

grep -r "root" .
terdon
  • 242,166