0% found this document useful (0 votes)
6 views6 pages

ShellScripting Part3

The document contains a series of shell scripting problems and solutions related to file manipulation in UNIX. It includes tasks such as copying lines from a file, separating odd and even lines, counting specific job titles based on salary, and generating student information based on user input. Each problem is accompanied by a detailed shell script solution demonstrating various scripting techniques.

Uploaded by

coolaryan.rao
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

ShellScripting Part3

The document contains a series of shell scripting problems and solutions related to file manipulation in UNIX. It includes tasks such as copying lines from a file, separating odd and even lines, counting specific job titles based on salary, and generating student information based on user input. Each problem is accompanied by a detailed shell script solution demonstrating various scripting techniques.

Uploaded by

coolaryan.rao
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Birla Institute of Technology & Science - Pilani,

K. K. BIRLA Goa campus


Shell Scripting
(UNIX – Managing with Files)

Problem 1:
Write a shell script program to copy numLine number of lines from the
starting line number startNum of the file named [Link]. Store the
copied lines in a file named [Link]. Take numLine and startNum
as user input. [Use the file [Link] available in ftp for doing the
same].

Method I
echo "Enter the start line number:”
read startNum
echo “Enter the number of lines to be displayed:”
read numLine
endLine=$((startLine + numLine – 1))
head -$endLine [Link] | tail -$numLine > [Link]

Method II
echo "Enter the start line number:"
read startNum
echo "Enter the number of lines to be displayed:"
read numLine
endLine=`expr $startNum + $numLine - 1`

i=1
while read line
do
if [ $i -gt $endLine ]
then
break
fi

if [ $i -ge $startNum ]
then
echo $line >> [Link]
fi

i=`expr $i + 1`

done<[Link] # Reads line by line from [Link]


Problem 2:
Copy the odd lines of the file named [Link] to a file named [Link]
and copy the even lines of the file named [Link]

i=1

while read line


do
if [ `expr $i % 2` -eq 0 ]
then
echo $line >> [Link]
else
echo $line >> [Link]
fi

i=`expr $i + 1`

done<[Link]

Problem 3:
Given a file named [Link] with employID, employName, employSurName,
Post, Department, DOB and Salary. Write a shell script program to
find the number of GMs in the company whose salary is greater than
200000.

cut -d " " -f4,7 [Link] > [Link] # Copies the 4th column & 7th
# column of [Link] and save it
# in [Link]
x="GM"
count=0

while read position salary # read position and salary from [Link]
do
if [ "$position" = "$x" -a $salary -gt 200000 ]
then
count=`expr $count + 1`
fi

done < [Link]

echo $count
Problem 4:
Given a file named [Link] with employID, employName, employSurName,
Post, Department, DOB and Salary. Write a shell script program to
replace all the GMs in the company whose salary is less than 200000
to Manager.

cut -d " " -f4,7 [Link] > [Link]


x="GM"
count=0

while read position salary


do
if [ "$position" = "$x" -a $salary -lt 200000 ]
then
echo "Manager" >> [Link]
else
echo $position >> [Link]
fi

done < [Link]

cut -d " " -f1,2,3 [Link] >newemp1_3.txt


cut -d " " -f5,6,7 [Link] >newemp5_7.txt
paste newemp1_3.txt [Link] newemp5_7.txt > [Link]
# paste command copies vertically the contents of the files mentioned

Problem 5:
[You can find a sample timetable in the file named [Link] in
ftp. Use [Link] to solve this problem.] Write a shell script
program to read the hour and day from the user and display the
subject corresponding to hour and day.

echo "Enter Day: MON to SAT"


read day
echo "Enter Hour: T1 to T9"
read hour
flag=0
while read line
do
len=${#line} # this command will give the length of the line
hno=${hour:1:1} # Example: ${hour:x:y} will give y characters
# starting from xth location. [first character
# is in 0th location]
if [ ${#hour} -ne 2 ]
then
echo "Wrong Hour"
break
fi
if [ ${#day} -ne 3 ]
then
echo "Wrong Day"
break
fi
echo "hno = $hno"
i=0
space=0
while [ $i -le $len ]
do
if [ "${line:$i:1}" = " " -o $i -eq $len ]
then
space=`expr $space + 1`
if [ $space -eq 1 ]
then
j=`expr $i - 1`
day1=${line:0:$i}
if [ "$day1" != "$day" ]
then
break
fi
fi
if [ $space -eq `expr $hno + 1` ]
then
subLen=`expr $i - $startIndex`
echo ${line:$startIndex:$subLen}
flag=1
break
else
startIndex=$i
fi
fi
i=`expr $i + 1`
done
if [ $flag -eq 1 ]
then
break
fi
done < [Link]

Problem 6:
[You can find 2 files named [Link] and [Link] in ftp. The file
[Link] contains student ID and their names. The file [Link]
contains student ID, their age and DOB. Use these files for solving
this problem.] Write a shell script program to read the name of a
student from the user and store the entire student information
[Student ID, Name, age, DOB and the student’s bits mail id] in to a
new file named [Link] if the year of birth of the student is
greater than 85. If the year of birth is less than 85 then print a
message "year of birth is less than 85". [Create the bits email id by
extracting the year of admission and serial no from the student ID].
If the name is not match print the message "data not found".

echo "Enter the name of the student:"


read name

while read line1


do
lineLen1=${#line1}
nameLen1=`expr $lineLen1 - 12`
name1="${line1:13:$nameLen1}"
if [ "$name" = "$name1" ]
then
id="${line1:0:12}"
while read line2
do
sid="${line2:0:12}"
year=${line2:22:2}
lineLen2=${#line2}
remLen=`expr $lineLen2 - 13`
if [ "$sid" = "$id" -a $year -gt 85 ]
then
email="f${line1:0:4}${line1:8:3}@[Link]"
echo "$id $name ${line2:13:remLen} $email"
break
fi
if [ "$sid" = "$id" -a $year -le 85 ]
then
echo "Year of birth is less than 85"
break
fi
done < [Link]
if [ "$sid" != "$id" ]
then
echo -n "$id $name "
echo "Other Information NOT present"
fi
break
fi
done <[Link]
if [ "$name" != "$name1" ]
then
echo "Entered Name is not available in File"
fi
Problem 7:
[Create a text file called [Link] and store some random
numbers in it (one number in one line).]
Write a shell script program to display all the even numbers from the
file.

while read line


do
x=`expr $line % 2`
if [ $x -eq 0 ]
then
echo $line
fi
done < [Link]

Exercise
Q1. Write a shell script to find all prime numbers from
[Link] file and store in another file called [Link]

Q2. Write a shell script to find largest number and smallest


number from [Link] file.

You might also like