-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

closed as too broad by Jenny D, a CVn, slm, Caleb, goldilocks Jun 17 '14 at 14:24

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 & Linux, 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
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" .
  • 1
    Thanks and I'm so sorry. I couldn't able to log in for a long time. – user72789 Aug 8 '14 at 4:47
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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.