TCS Java Hackathon - UNIX Hands-on
Q1. Unix : Count occurrence of word
Write the unix command to count the occurrence of the word "Unix" in a given file.
The file will be given as command line argument while the script containing your command will be run.
Note : The search for "Unix" should be case-insensitive.
For example,
If the input file contains the following lines
Unix is an multi-user,multi-tasking system.
It is a command based operating system.
We will learn unix architecture and the unix commands in this module.
Then the output will be,
3
Solution
grep -o -i unix | wc -l
Q2. Unix : Word Count
Write the unix command to count the number of words in the first 3 lines of a file.
The file will be given as a command line argument when the script containing your command will run.
For example,
If the input file contains the following lines
Unix is a command based operating system.
We will learn unix in this module.
This is a test file.
We are using this file to practice some commands.
We have reached the end of the file.
The output will be,
19
Solution
head -3|wc -w
Q3. Unix : Find sum of even numbers
Write a shell script to find the sum of all even numbers from a list of given numbers. The script should first of all take the count of numbers to be added as user input followed by the numbers one by one.
The output should print the following :
Total = <Sum>
Console Input :
The input needs to be provided as -
The first line contains the count of numbers to be added.
The second line contains the 1st number to be added.
The third line contains the 2nd number to be added. and so on.
For example,
if we want to provide 10, 20 and 30 as the numbers then provide the input as
3
10
11
30
The output for this example will be
Total = 40
Solution
read n
awk '
BEGIN {
sum=0;
}
{ if ( $0%2==0 ){
sum+=$0;
}
}
END { print "Total","=",sum}'
Q4. Unix : Highest Score
Student details are stored in a file in the following order with space as the delimiter:
RollNo Name Score
Write a unix command to find the name of the Student who has the highest score.
The file will be given as a command line argument when the script containing your command will run.
For example,
If the input file has the below content
RollNo Name Score
234 ABC 70
567 QWE 12
457 RTE 56
234 XYZ 80
456 ERT 45
The output will be
XYZ
Solution
sort -k3,3 -rn -t" " | head -n1 | awk '{print $2}'
Q5. Unix : Average Salary
Employee Details are stored in a file in the following format:
EmpID;EmpName;Salary
Write a shell script to find the count of employees whose salary is less than the average salary of all employees.
The file with the employee details will be given as a command line argument when your script will run.
For example,
If the input file contains the following employee record
EmpID;EmpName;Salary
100;A;30000
102;B;45000
103;C;15000
104;D;40000
The output will be,
2
Solution
read
awk 'BEGIN{ FS=";";Total;count;i;num}
{
sum+=$3;num+=1;a[i++]=$3;
}
END{ avg=sum/num;
for(j=0;j<i;j++){
if(a[j]<avg){
count++
}
}
{print count}
}'
Comments
Post a Comment