Unit - Hackerrank Activities - Unix
1. 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
2. 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
3. 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
Solutions:
read n
awk '
BEGIN {
sum=0;
}
{ if ( $0%2==0 ){
sum+=$0;
}
}
END { print "Total","=",sum}'
4. 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}'
5. 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 records
EmpID;EmpName;Salary
100;A;30000
102;B;45000
103;C;15000
104;D;40000
The output will be,
2
Solutions:
lim=10
max=0
salary=0
read
n=0
#count the avg plus store in array
for i in $(seq $lim)
do
read line
num=$(echo "$line"|cut -d ";" -f3)
if [ $num -gt 0 ];
then
salary=$((salary+num))
arr[n]=$num
n=$((n+1))
fi
done;
avg=$((salary/n))
#count now
count=0
for val in ${arr[@]}
do
if [ $val -lt $avg ];
then
count=$((count+1))
fi
done;
echo "$count"
awk 'BEGIN {FS = ";"} ; {s += $3; ++c;a[$3] += $3;k[$1] += $1} END{ for (i in a) if(i<s/(c-2)) p+=1;for (j in k);if(j == 5) {print(p+1)} else if(j == 6) {print(p)} else {print(p-1)}}'