You are on page 1of 23

UNIT 4

1 MOHIT ARORA
1. SIMPLE FILTERS
 

PAGINATING FILES

The pr command prepares a file for printing by adding suitable headers, footers and formatted
text.

$ pr emp. Lst

Head command
 
It helps in slicing horizontally from the top of the file. i.e. displays the number of lines
specified in the beginning of the file.

$ head -n 3 emp . lst

2 MOHIT ARORA
Tail command
 
It helps in slicing horizontally from bottom of the file. i.e. displays the number of lines
specified in the end of the file.
 
 $ tail -n 3 emp . lst

Cut command
 
It helps in slitting vertically using cut command. It identified both columns and fields
and is very useful filter for the programmer.
 
$ head -n 5 emp . lst | tee shortlist

3 MOHIT ARORA
Paste command
 
It is a special type of concatenation. It pastes files vertically, rather than horizontally.
As whatever you cut with the cut command can be pasted back using the paste
command.
 
 $ paste cutfile pastefile

Sort command
 
It arranges each line of the input in the  ascending order.

$ sort shortlist

 U
ni
q

command

4 MOHIT ARORA
 
 It helps in selecting non-repeating lines
 
$ uniq abc.

Tr command
 
It translates one set of character into another. It is used to squeeze the repeated
occurrences of a character into one character.

$ tr ‘ | / ‘ ‘~ - ‘ < emp.lst | head -n 3

 
Grep command
 

5 MOHIT ARORA
It is used for globally search for regular expression and print out.
It searches the file for a particular pattern of characters and displays all lines that
contain that pattern. The pattern that is searched in the file is referred to as the regular
expression.
 
$ grep “sm” emp36

The options used with grep command are:


 
-i : it is used for ignoring the case.

$ grep –i ‘agarwal’ emp.lst

 -c : it is used for counting the number of occurrences  .

6 MOHIT ARORA
$ grep –c ‘director’ emp.lst

-n : it is used to display the line numbers containing the pattern, along with the


lines..

$ grep –n ‘marketing’ emp.lst

 -v : it is used for deleting the lines.

$ grep –v ‘director’ emp.lst >otherlist

 -l : it display filenames containing patterns.

7 MOHIT ARORA
$ grep –l ‘manager’ *.lst

 -e : with this you can match different names as agarwal,aggarwal

$ grep –e “Agarwal” –e “aggarwal” –e “agrawal” emp.lst

 
 

8 MOHIT ARORA
 FIND command
 
It is used to locate a file in a particular directory and all its sub-directories. It is the
command used for locating files and has various options to make advance searches. It
displays files only when print option is given
 
Syntax: find <path list> <selection criteria> <action>
 
For e.g.:
 
$ find / -name .afedt.buf –print
 
The options used with find command are:
 
-name flname : selects file flname
-user usname : selects file if owned by usname
-type f            : selects file if ordinary file
-type d           : selects file if directory
-group gname : selects file if owned by group gname
-atime +x         : selects file if accessed in more than x days
-amin -x          : selects file if accessed in more than x minutes
-mtime -x        : selects file if modified in less than x days
-amin -x          : selects file if modified in less than x minutes
 
For e.g.: find the list of those files that have been modified in less than 2 days
$ find . –mtime –2 -print
 
The operators used are –a (AND) and –o (OR)
 
For e.g.: find the perl and shell scripts files that have been modified in one month.
$ find  /home  –mtime  +30 \  ( -name  “*.sh”  –o  –name  “*.pl” \ ) –print
 

9 MOHIT ARORA
2. SHELL SCRIPT

QUES 1) To input the age of a person and then display is he is a minor, young
man or an old man.
echo enter the age

read age

if [ $age –lt 18 ]

then

echo the person is minor

elif [ $age –lt 30 ]

then

echo the person is young

elif [ $age –lt 50 ]

then

echo the person is old

fi

10 MOHIT ARORA
11 MOHIT ARORA
QUES 2) To accept a pattern and filename from the user, search for the pattern
in the desired filename and then display it to the user.

echo enter the pattern

read pattern

echo enter the filename

read filename

a= `grep $pattern $filename`

echo $a

12 MOHIT ARORA
QUES 3) To input a character from the user, check if it is in uppercase,
lowercase, a numeral of a special character and then display it to the user.

echo enter a character

read character

case $character in

([a-z]) echo it is in small case;;

([A-Z]) echo it is in upper case;;

([0-9]) echo it is a digit;;

(?) echo it is special character;;

esac

13 MOHIT ARORA
14 MOHIT ARORA
QUES 4) To generate the Fibonacci series.

A=0

B=1

Echo “enter the limit”

Read n

Echo $a

Echo $b

For <<i=0;i<&n-2;i++>>

Do

C=”expr $a +$b”

Echo $c

A=’expr $b’

B=’expr $c’

done

15 MOHIT ARORA
16 MOHIT ARORA
QUES 5) To calculate the factorial of all inputed number.

Echo ‘enter number whose factorial is required’

Read n

F=1

For <<i=1;i<=n;++i>>

Do

F=’expr $f\* $ i’

Done

Echo $ f

17 MOHIT ARORA
18 MOHIT ARORA
QUES-6: Write a shell script to make a even driven menu for the arithmetic
calculations?

Echo enter the first number

Read n1

Echo enter the second number

Read n2

Echo 1 . addition of two numbers

Echo 2. Subtraction of two numbers

Echo 3.multiplication of two numbers

Echo 4. Division of two numbers

Echo enter your choice

Read ch

Case $ch in

1>a=’expr $n1+$n2’

Echo the result of addition is: $a;;

2> b=’expr $n1-$n2’

Echo the result of subtraction is: $b;;

3>c=’expr $n1\ * $n2’

Echo the result of multiplication is: $c;;

4>d=’expr $n1/ $n2’

Echo the result of division is: $d;;

*> echo wrong choice

Esac

19 MOHIT ARORA
20 MOHIT ARORA
QUES-7:Write a shell script to list all the sub-directories in the current

directory?
echo 1. list subdirectory

echo 2. exit

echo enter your choice

read ch

case $ch in

1)ls;;

2)exit;;

*)echo wrong choice

esac

21 MOHIT ARORA
QUES-8:Write a shell script to compare the contents of two files and if they
are same then delete the second file?
echo Enter The First File

read file 1

echo Enter The Second File

read file 2

cmp –s $file1 $file2

x= `echo $? `

if [ $x –eq 0 ]

then

rm $file2

echo File Removed : $file2

else

echo Files Are Different

fi

22 MOHIT ARORA
QUES-9: Write ascript which display the options as

 display time
 display date and month and year
 display day of week

echo”----------------------main menu----------------“

echo “display time”

echo “display date and month and year”

echo “display day of week”

echo” enter your choice”

read ch

case $ch in

1)date +%r;;

2date +%D;;

3)date +%A;;

esac

23 MOHIT ARORA

You might also like