Professional Documents
Culture Documents
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.
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.
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
6 MOHIT ARORA
$ grep –c ‘director’ emp.lst
-n : it is used to display the line numbers containing the pattern, along with the
lines..
7 MOHIT ARORA
$ grep –l ‘manager’ *.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
then
then
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.
read pattern
read 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.
read character
case $character in
esac
13 MOHIT ARORA
14 MOHIT ARORA
QUES 4) To generate the Fibonacci series.
A=0
B=1
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.
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?
Read n1
Read n2
Read ch
Case $ch in
1>a=’expr $n1+$n2’
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
read ch
case $ch in
1)ls;;
2)exit;;
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
read file 2
x= `echo $? `
if [ $x –eq 0 ]
then
rm $file2
else
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----------------“
read ch
case $ch in
1)date +%r;;
2date +%D;;
3)date +%A;;
esac
23 MOHIT ARORA