Q1: echo no of arguments are $# echo arguments are $* x=" " for n in $* do x="$n $x" done echo reverse order is $x

Q2: echo enter the file read filename echo file after removing multiple spaces is tr -s '\t' ' ' <$filename echo file after removing blank lines is sed '/^[ ]*$/d' $filename

Q4: count=1 while [ $count -eq 1 ] do echo MENU echo 1: To display the list of files echo 2: To display the permission of file

3) echo Enter string to be matched: read s echo Enter a for ignoring the case and b for case sensitive read ch1 case $ch1 in a) grep -i $s $1 .43-50 ..echo 3: To find the pattern in the file echo a:ignoring the case echo b:case sensitive echo 4: To replace all 'e' by 'a' echo Enter your choice read ch case $ch in 1) echo Files are: ls -fx .. 2) echo Permission of the files are: ls -l $1|cut -c1-10.

... of lines: read num . esac . 4) echo File after replacing 'e' by 'a' is: tr '[e]' '[a]'<$1 . *) Q5: count=1 while [ $count -eq 1 ] do echo MENU echo choice1: To display last n lines echo choice2: To sort in ascending or descending order echo Enter your choice: read ch case $ch in choice1) echo Enter the no. b) grep $s $1 ...

.... b) echo Sorted file in descending order is: sort -r f1 .. esac . choice2) echo enter a for ascending order and b for descending order: read ch1 case $ch1 in a) echo Sorted file in ascending order is : sort f1 . *) echo wrong choice .echo Last $num lines are: tail -n $num f1 . esac echo enter 1 to continue read count .

done Q6: hour=`date | cut -c 12-19` echo $hour if [ $hour -ge 0 -a $hour -lt 12 ] then echo 'Good Morning' elif [ $hour -ge 12 -a $hour -lt 16 ] then echo 'Good Afternoon' elif [ $hour -ge 16 -a $hour -lt 20 ] then echo 'Good Evening' else echo 'Good Night' fi Q7: echo Enter username read username echo users logged in are : ls -l|cut -c14-17 echo Number of times users logged in are : who|wc -l|uniq –c .

. 2) echo enter the text to append with dis file cat>> $filename echo file after appending is cat $filename .Q8: echo enter the file read filename if [ -f $filename ] then if [ -d $filename ] then echo nothing can be done else echo enter the choice echo 1:for overwrite echo 2:append the files read ch case $ch in 1) echo enter the text cat> $filename echo file after overwriting is cat $filename .

Hence remove the second one rm $file2 else echo files are different fi Q10: echo enter the name of file1 read file1 echo enter the name of file2 read file2 echo enter the name of file3 .. esac fi fi Q9: echo enter the name of first file read file1 echo enter the name of second file read file2 diff $file1 $file2>file3 if [ `echo $?` -eq 0 ] then echo files are same..

APR* |apr*) k=4 . ... MAY* |may*) k=5 .read file3 cat $file1 $file2 $file3>newfile echo result of merged file is cat newfile echo sorted output is sort newfile|more Q11: y=2012 for i in $* do k=$i case $k in JAN* |jan*) k=1 ... MAR* |mar*) k=3 . JUN* |jun*) k=6 .. FEB* |feb*) k=2 ..

.... NOV* |nov*) k=11 . *). AUG* |aug*) k=8 .. OCT* |oct*) k=10 .. MAR*|mar*) k=3 . .. SEP* |sep*) k=9 .JUL* |jul*) k=7 .. FEB*|feb*) k=2 ... esac cal $k $y done Q12: case $1 in JAN*|jan*) k=1 . DEC* |dec*) k=12 .

. JUL*|jul*) k=7 .. NOV*|nov*) k=11 . SEP*|sep*) k=9 .. JUN*|jun*) k=6 .APR*|apr*) k=4 . MAY*|mar*) k=5 . DEC*|dec*) k=12 ... MAR*|mar*) k1=3 ... AUG*|aug*) k=8 . FEB*|feb*) k1=2 . esac case $3 in JAN*|jan*) k1=1 .... OCT*|oct*) k=10 ..

j++) { if(a[i]==a[j]) { .. JUL*|jul*) k1=7 . JUN*|jun*) k1=6 .. MAY*|may*) k1=5 . AUG*|aug*) k1=8 Q13: BEGIN{i=0} { a[i]=$0 i++ n=i } END{ for(i=0.i<n-1. APR*|apr*) k1=4 .j<n.i++) { for(j=i+1.....

i<=n.i++) { for(j=1.a[i] } } Q14: BS BEGIN{n=1} { a[n]=$1 n=n+1 } END{ for(i=1.i<n.j<n.i++) { printf "%s\n".a[j]=" " } } } printf "After removing duplicate lines\n" for(i=0.j++) { if(a[j]>a[j+1]) { temp=a[j] .

i<=NR.i++) { value=line[i] j=i-1 while((j>0) && (line[j]>value)) { line[j+1]=line[j] j-} line[j+1]=value } for(i=1.i++) print a[i] } Q14: IS { line[NR] = $0 } END{ for(i=1.a[j]=a[j+1] a[j+1]=temp } } } printf "Sorted array is \n" for(i=1.i<=n.i++) .i<=NR.

NR) t=line[i] line[i]=line[mi] line[mi]=t } for(i=1.i.j++) { if(a[j]<min) { min=a[j] m=j }} return m } {line [NR] = $0} END{ for(i=1.print line[i] } Q14: SS function getminindex(a.s) { min=a[i] m=i for(j=i+1.i<=NR.j<=s.i<=NR.i++) .i++) {mi=getminindex(line.i.

31.line else { while(length(line)>30) { printf "%s\n".30) line=substr(line.substr(line.1.{ print line[i] }} Q15: { line=$0 if(length(line)<=30) printf "%s\n".line } } .length(line)-30) } printf "%s\n".