Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

awk -F “|” ‘/Surati/ { printf “%2d %4d %-20s \n”, NR, $1,$2 }’ student

awk -F “|” ‘{ printf “%4d %20s %2d %2d %2d %3d %3.2d \n”, NR, $1,$2,$4,$5,$6,$4+$5+$6,($4+$5+
$6)/7 }’ mark

awk -F “|” ‘{printf “%4d %20s %2d %2d %2d %3d %3.2d \n”,
$1,$2,$4,$5,$6,$4+$5+$6,($4+$5+$6)/7 }’ mark

awk -F “|” ‘{printf “%4d %-20s %2d %2d %2d %3d %3.2d \n”,
$1,$2,$4,$5,$6,$4+$5+$6,($4+$5+$6)/7 }’ mark

awk -F “|” ‘$1>2000 {printf “%4d %-20s %2d %2d %2d %3d %3.2d \n”, $1,$2,$4,$5,$6,$4+$5+$6,($4+
$5+$6)/7 }’ mark

awk -F “|” ‘BEGIN {print "Marklist"} $1>2000 {printf “%4d %20s %2d %2d %2d %3d %3.2d \n”,
$1,$2,$4,$5,$6,$4+$5+$6,($4+$5+$6)/7 }’ mark

awk -F “|” ‘BEGIN {print "Marklist"} $1>2000 {printf “%4d %20s %2d %2d %2d %3d %3.2d \n”,
$1,$2,$4,$5,$6,$4+$5+$6,($4+$5+$6)/7} END {print "the end"}’ mark

awk -F “|” ‘BEGIN {print "Marklist"} $1>2000 {c++;printf “%d %4d %20s %2d %2d %2d %3d %3.2d \
n”,c,$1,$2,$4,$5,$6,$4+$5+$6,($4+$5+$6)/7} END {print "the end"}’ mark

awk -F “|” ‘BEGIN {print "Marklist"} $1>2000 {c=c+1;printf “%d %4d %20s %2d %2d %2d %3d %3.2d \
n”,c,$1,$2,$4,$5,$6,$4+$5+$6,($4+$5+$6)/7} END {print "the end";printf "Total records:%d\n",c}’
mark

Script to create simple menus and take action according to that selected
# menu item
#
while :
do
clear
echo "-------------------------------------"
echo " Main Menu "
echo "-------------------------------------"
echo "[1] Show Todays date/time"
echo "[2] Show files in current directory"
echo "[3] Show calendar"
echo "[4] Start editor to write letters"
echo "[5] Exit/Stop"
echo "======================="
echo -n "Enter your menu choice [1-5]: "
read yourch
case $yourch in
1) echo "Today is `date` , press a key. . ." ; read ;;
2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;;
3) cal ; echo "Press a key. . ." ; read ;;
4) vi ;;
5) exit 0 ;;
*) echo "Opps!!! Please select choice 1,2,3,4, or 5";
echo "Press a key. . ." ; read ;;
esac
done

To print last word of each line

BEGIN {OFS="|"}

print $NF

Runawk -f lw.awk a1

write a shell script that will read two numbers from command line and display all even numbers
between two numbers.

echo -n "first number : "

echo $1

echo -n "second number : "

echo $2

n1=$1

n2=$2
while test $n1 -lt $n2

do

# see if it is odd or even number

test=$(( $n1 % 2 ))

if test $test -eq 0

then

echo $n1

fi

n1=`expr $n1 + 1`

done

Runsh a1.sh 1 10

Write A Script To Perform Following String Operations Using Menu:


1. COMPARE TWO STRINGS.
2. JOIN TWO STRINGS.
3. FIND THE LENGTH OF A GIVEN STRING.
4. OCCURRENCE OF CHARACTER AND WORDS
5. REVERSE THE STRING.

clear

choice=y

while [ "$choice" = "y" ]

do

echo "____________________________________________"

echo "1. COMPARE TWO STRINGS"

echo "2. JOIN TWO STRINGS"

echo "3. FIND THE LENGTH OF A GIVEN STRING"

echo "4. OCCURRENCE OF CHARACTER AND WORDS"


echo "5. REVERSE THE STRING"

echo "6. EXIT"

echo "____________________________________________"

echo "Enter Choice: \c"

read ch

echo "____________________________________________"

case $ch in

1)

echo "Enter String1: \c"

read str1

echo "Enter String2: \c"

read str2

if test $str1 = $str2

then

echo "String is equal"

else

echo "String is not equal"

fi

;;

2)

echo "Enter String1: \c"

read str1

echo "Enter String2: \c"

read str2
str3=`echo $str1$str2`

echo $str3

;;

3)

len=0

echo "Enter String1: \c"

read str1

len=$(echo "$str1" | wc -c)

len=`expr $len - 1`

echo "Length: " $len

;;

4)

echo "Enter String: \c"

read str

echo "Enter Word to find : \c"

read word

echo $str | cat > str1.txt

grep -o $word str1.txt | cat > str2.txt

count=`grep -c $word str2.txt`

echo $count

;;

5)

echo -e "Enter String1: \c"

read str
len=`expr $str | wc -c`

len=`expr $len - 1`

while test $len -gt 0

do

rev=`expr $str | cut -c $len`

ans=`expr $ans$rev`

len=`expr $len - 1`

done

echo $ans

;;

6) exit ;;

*) echo "Invalid Choice ....." ;;

esac

echo "Do u want to continue.....? [y/n]"

read choice

case $choice in

Y|y) choice=y;;

N|n) choice=n;;

*) choice=y;;

esac

done
Write a shell script to display hour in time in word.

clear

echo "Current Time is: " `date +%r`

h=`date +%H`

m=`date +%M`

s=`date +%S`

echo "Hour is "$h

echo "Minute is "$m

echo "Seconds are "$s

case $h in

1)echo "ONE ";;

2)echo "TWO ";;

3)echo "THREE ";;

4)echo "FOUR ";;

5)echo "FIVE ";;

6)echo "SIX ";;

7)echo "SEVEN ";;

8)echo "EIGHT ";;

9)echo "NINE ";;

10)echo "TEN ";;

11)echo "ELEVEN ";;

12)echo "TWELVE ";;

*)echo "Invalid ";;

Esac

Write a Shell Script to delete the zero sized file using if and for.
clear

echo "Enter any file name: "

read filenm

if [ -e $filenm ]

then

echo $filenm" File exist"

if [ -s $filenm ]

then

echo $filenm" File has size > 0"

else

rm $filenm

echo $filenm" File is Deleted which has size = 0"

fi

else

echo "File not exist"

fi

Write a shell script to display all users names, or users names of a


particular group, or name of users who are currently working in the
system.

i="y"

while [ $i = "y" ]

do

echo "1. TO DISPLAY ALL USERS NAMES."

echo "2. USERS NAME OF PARTICULAR GROUP."


echo "3. NAME OF USERS WHO ARE CURRENTLY WORKING ON THE SYSTEM."

echo "4. EXIT"

echo "ENTER A CHOICE:"

read choice

case $choice in

1)echo "ALL USERS NAMES ARE:"

cut -d ":" -f 1 /etc/passwd | more

read choice;;

2)echo "ENTER A PARTICULAR GROUP NAME:"

read g1

grep "$g1" /etc/passwd | cut -d ":" -f 1

read choice;;

3)who | more

read choice;;

4) exit;;

esac

echo "Do u want to continue ? "

read i

if [ $i != "y" ]

then

exit

fi

done
Write a script to display the name of those files (in the given directory)
which are having multiple links

echo -e "-: DISPLAY FILES HAVING MULTIPLE LINKS :-"

echo -e "DIRECTROY PATH : \C"

read dirpath

ls -l $dirpath| tr -s " ""|" > flinks

grep -v "|1|" flinks | cut -d"|" -f9 > fname

echo -e " files having multiple in $dirpath are "

check flink file

1. display line starting from 10th line to end of x1 file


sed –n ’10,$p’ stud
2. Display all files in current directory where first character is numeric and last character is not
alphabetic.
Ls|grep –E ‘^[0-9]+[^a-zA-Z]’
3. Display content of last modified file
Ls –ltu|head –n 2 |tail –n 1 | tr –s ‘ ‘ |cat `cut –d “ “ –f 9`
4. To display content of top three large file
Cat ` ls –pS|grep –v /| head –n 3`
5. Display those files from current directory whose 1st character is not digit
Ls| grep ‘^[^0-9]’
6. Display inode number with from current directory
Ls –li
7. Display all files which have read and write permission for the group.
Ls –l |grep ‘^-…rw….’
8. Find out the number of the character '?' occur in a file fl .txt.
Grep –o ‘\?’ x1|wc -l
9. Count number of words in line 40 thought 60 of file fl .txt.
Sed –n ’40,60p’ stud| tr “ “ “\012”|wc -l
10. To delete all vowels from the file xl.txt.
Tr –d [aeiouAEIOU]<x1
11. Replace the word unix with UNIX in between 5th to 10th line including both.
Sed –n ‘5,10p’ x1|sed ‘s/unix/UNIX/’
12. Display those words whose length greater than 10 characters and consist of alphabet only
Tr “ “ “\012”<x1|awk 'length($NF)>5 {print}'|awk '/^[a-zA-z]+$/'
13. Print odd numbers of words in each line

awk ' NR % 2 == 1 {print NR}' stud


14. Count occurrences of pattern Opera file fl.
awk '/Opera/ {c++} END {print c}' x1
15. Display number of process
Ps aux|wc –l
16. Display line 10 to 15 from file XI.
Sed –n ‘1-,15p’ x1
17. Display line before a line that contains pattern xyz in fl.txt.
grep -A1 'Surat' stud | grep -v 'Surat'
18. To display those lines in between 25 and 50 having pattern 'unix’ in it.
sed -n '2,5p' stud | grep 'unix'
19. Display the lines which are not starting with 2 at the beginning.
Grep ‘^[^2]’ stud
20. To remove all leading space from FILE
sed 's/^ */ /g' stud
21. Display system process only
Ps –ef
22. Display last 40 line using awk

Awk ‘NR>c-40’ c=$(wc –l<stud) stud

23. Display 20 to 40 line using awk(between)

Awk ‘NR>c-40 && NR<21’ c=$(wc –l<stud) stud

24. Display user name who logged in more than once

Finger | tr –s ‘ ‘|cut –d “ “ –f 1| uniq –c|tr –s ‘ ‘|grep –v “^ 1”

You might also like