Sei sulla pagina 1di 15

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

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,43-50 ;; 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

;; b) grep $s $1 ;; 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. of lines: read num

echo Last $num lines are: tail -n $num f1 ;; 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 ;; b) echo Sorted file in descending order is: sort -r f1 ;; esac ;;

*) echo wrong choice ;; 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

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 ;; 2) echo enter the text to append with dis file cat>> $filename echo file after appending is cat $filename

;; 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.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

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 ;; FEB* |feb*) k=2 ;; MAR* |mar*) k=3 ;; APR* |apr*) k=4 ;; MAY* |may*) k=5 ;; JUN* |jun*) k=6 ;;

JUL* |jul*) k=7 ;; AUG* |aug*) k=8 ;; SEP* |sep*) k=9 ;; OCT* |oct*) k=10 ;; NOV* |nov*) k=11 ;; DEC* |dec*) k=12 ;; *);; esac cal $k $y done

Q12: case $1 in JAN*|jan*) k=1 ;; FEB*|feb*) k=2 ;; MAR*|mar*) k=3 ;;

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

;; APR*|apr*) k1=4 ;; MAY*|may*) k1=5 ;; JUN*|jun*) k1=6 ;; JUL*|jul*) k1=7 ;; AUG*|aug*) k1=8

Q13: BEGIN{i=0} { a[i]=$0 i++ n=i } END{ for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(a[i]==a[j]) {

a[j]=" " } } } printf "After removing duplicate lines\n" for(i=0;i<n;i++) { printf "%s\n",a[i] } }

Q14: BS BEGIN{n=1} { a[n]=$1 n=n+1 } END{ for(i=1;i<=n;i++) { for(j=1;j<n;j++) { if(a[j]>a[j+1]) { temp=a[j]

a[j]=a[j+1] a[j+1]=temp } } } printf "Sorted array is \n" for(i=1;i<=n;i++) print a[i] }

Q14: IS { line[NR] = $0 } END{ for(i=1;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<=NR;i++)

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

{ print line[i] }}

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

Potrebbero piacerti anche