In: Computer Science
Write a shell program named HELLO2(this should be done in linux,
using bash)
Consider files in the current directory whose names end in 't' or
'z'.
For such files only, your program should list certain LINES of the
file(s).
These lines are those that have at least 4 x's somewhere in the
line.
Note that the x's may be upper- or lower-case, and may be separated
by
other characters; so the following 3 lines DO match:
XXxX and more things
ABC xyzxXd efX abc xSx
xxxx
Your program should preface each line by the filename and a
colon.
Your program should not print any error messages, even no such
files exist.
==>Program
#! /bin/sh
option1='t'
option2='z'
required_count=4
for i in $PWD/*
do
file_name=$(basename $i)
file_name=$(file_name%%.*}
last_letter=${file_name: -1}
if [ $last_letter = $option1 -o $last_letter = $option2 ]
then
while read line; do
count_x = $(grep - o "[x|X]" <<< "$line" | wc -l)
if [ $count_x -ge $required_count ]
then
echo "$file_name : $line"
fi
done < $i
fi
done
------------------------------------------
In this program first we get the current directory by $PWD then we loop through each file present in current directory ...we first extracted the file name with extension and then extracted only the filename without extension ...and then the last letter of file name...
Then checked the condition as per given in question ... if its true then we loop through each line of file and count occurence of letter x + X by using grep command and then if its greater than 3 we output it as desired in question..