Reduce the number of lines in script

Hello All,

I have created a script which will show the errors from a log file in between a particular section.

v1=$(sed -n "/Main Report/,/Main Report End/p" input | grep -i 'Unable to find'
v2=$(sed -n "/Main Report/,/Main Report End/p" input | grep -i 'Unable to add'

if [ -z "$v1" ]
then
echo "All report deployed properly"
else
echo "List of report failed to deploy in Main Report due to Null pointer exception"
echo "$v1"
fi 

if [ -z "v2"]
then
echo "All Report Deployed Properly"
else
echo "List of report failed to deploy in Main Report due to xml issue"
echo "$v"
fi 

v3=$(sed -n "/Second Report/,/Second Report End/p" input | grep -i 'Unable to find'
v4=$(sed -n "/Second Report/,/Second Report End/p" input | grep -i 'Unable to add'

if [ -z "$v3" ]
then
echo "All report deployed properly"
else
echo "List of report failed to deploy in Second Report due to Null pointer exception"
echo "$v3"
fi 

if [ -z "v4"]
then
echo "All Report Deployed Properly"
else
echo "List of report failed to deploy in Second Report due to xml issue"
echo "$v4"
fi 

Is there a way I can use some loop to reduce these code lines. The error are same but in different part of logs.

can you clear the scenario. from your code i understood that you are searching many files and trying to grep some string and printing report on that basis.
Please provide the file pattern and pattern to grep

Thanks for the reply.

Actually iam checking these error from a single log file. It consist around 900-1000 Lines. I need to check error staring from one section of report to the starting of other.for example take the below log.

aaaaaaaaa
bbbbbbbbbbb
Main Report
Unable to add file 1
Unable to add file 3
Unable to find file 2
Unable to find file 5
Main Report End
ttttttttttttt
uuuuuuuuuuu
Second Report
Unable to add file 56
Unable to add file 89
Unable to find file 484
Unable to find file 899
Second Report End

So in this way i have to find from the same log that how many files failed due to Unable to add and Unable to find reason in between Main Report start and End Section and Second Report start and End section.

---------- Post updated at 10:31 AM ---------- Previous update was at 09:49 AM ----------

Also iam getting values in variable in single line like the below

echo $v1
Unable to add file 56 Unable to add file 89

But iam looking for the below one

Unable to add file 56
Unable to add file 89
echo $v1

changes newlines to spaces.
In your script you did it correctly

echo "$v1"

In your script you have several times "all reports"; shouldn't that be the "specific report"? Or do you want to set an error variable, and at the end say "all reports okay" if the variable is not set?
A loop can shorten your script, but in this case I would go for a function...

Hello,

Not need to use grep, sed can search pattern and awk will better solution.
But, if we rest in shell:

$ cat file.sh
#!/bin/bash

while IFS=':' read m1 m2 p1 r1
do
        xx=$(sed -n "/$m1/,/$m2/{/$p1/p}" file.log)
        if [[ -z "$xx" ]]
        then
                echo "All report deployed properly"
        else
                echo "$r1"
                echo "$xx"
        fi
done <file.pat
$ cat file.pat
Main Report:Main Report End:Unable to find:List of report failed to deploy in Main Report due to Null pointer exception
Main Report:Main Report End:Unable to add:List of report failed to deploy in Main Report due to xml issue
Second Report:Second Report End:Unable to add:List of report failed to deploy in Second Report due to xml issue
Second Report:Second Report End:Unable to find:List of report failed to deploy in Second Report due to Null pointer exception
$ cat file.log
aaaaaaaaa
bbbbbbbbbbb
Main Report
Unable to add file 1
Unable to add file 3
Unable to find file 2
Unable to find file 5
Main Report End
ttttttttttttt
uuuuuuuuuuu
Second Report
Unable to add file 56
Unable to add file 89
Unable to find file 484
Unable to find file 899
Second Report End
$ ./file.sh
List of report failed to deploy in Main Report due to Null pointer exception
Unable to find file 2
Unable to find file 5
List of report failed to deploy in Main Report due to xml issue
Unable to add file 1
Unable to add file 3
List of report failed to deploy in Second Report due to xml issue
Unable to add file 56
Unable to add file 89
List of report failed to deploy in Second Report due to Null pointer exception
Unable to find file 484
Unable to find file 899

Regards.

This is with a function.
It stores the errors in variables, so needs more memory but reads the input file less often.

function print_error() {

v=$(sed -n "/$1/,/$1 End/p" input | grep -i 'Unable to ')

v1=$(echo "$v" | grep -i ' to find')
v2=$(echo "$v" | grep -i ' to add')

if [ -n "$v1" ]
then
  echo "List of report failed to deploy in $1 due to Null pointer exception"
  echo "$v1"
  err=1
fi

if [ -n "$v2" ]
then
  echo "List of report failed to deploy in $1 due to xml issue"
  echo "$v2"
  err=1
fi

if [ -z "$err" ]
then
  echo "All Report Deployed Properly"
fi

}

print_error "Main Report"
print_error "Second Report"