Tuesday, 12 November 2013

UNIX Bash/ Shell scripting examples and excercises


OUTPUT Redirection  in bash shell


  • 1> can be shortened to just >
  • 1>foo 2>&1 to >&foo or &>foo
  • 2>&1 | program to |& program


Unix Bash/ Shell scripting  examples  and   exercises

++Note : The answers below are correct to the best of my understanding .They may vary according to what is expected by the instructor.


1) 

What will be the output of the following ?


grep '^mo.*ing$' /usr/share/dict/words
Ans : all lines starting with 'mo' and ending with 'ing'
(if we count it is 153)

grep '[[:digit:]bc][^x-y]*$' /usr/share/dict/words

Ans :
grep '[[:digit:]bc]'--- will return all lines containing 0-9 OR b OR c



2)
Find all5 char words from /usr/share/dict/words that begin with 'I' and end in 'a'.
Ans: grep '^I.*a$' /usr/share/dict/words

3)
Match lines containing the years 1992-2009 from a file named file.txt.

Ans : egrep '199[2-9]|200[0-9]' file

4)
Search for a 7 digit phone number , possibley with a spcace or a hyphen in the middle
like 123 4567 or 123-4567 ?
grep '[0-9][0-9][0-9][ -][0-9][0-9][0-9]' file
grep '[0-9][0-9][0-9][ -]*[0-9][0-9][0-9]' file --if you want to make the space and - optional






No comments:

Post a Comment