count and compare no of records in bash shell script.

consider this as a csv file.

H,0002,0002,20100218,17.25,P,barani
D,1,2,3,4,5,6,7,8,9,10,11
D,1,2,3,4,5,6,7,8,9,10,11
D,1,2,3,4,5,6,7,8,9,10,11
D,1,2,3,4,5,6,7,8,9,10,11
D,1,2,3,4,5,6,7,8,9,10,11
T,5
N

i want to read the csv file and count the number of rows that start with D and compare with the value(next value to T) row starts with T.

please provide me the unix shell script code.

there is only one T in whole file and number of rows having D. When you say compare ... what do you need ?
D having maximum rows and if it is greater than number in T ?

Check this post

Are you trying something similar ?

awk -F "," '$1 == "D"{cnt+=1};$1 == "T"{tot=$2}END{print "Total in T is:",tot;if(cnt = tot) print "Same count and Count number is:",cnt; else print "Total not equal",tot}' file

cheers,
Devaraj Takhellambam

I wrote the following script with my understanding to your question. Just try it.
You didn't tell what we need to do after match the T value with no of rows starting with "D".
So just I print one statement after match the value.

#!/bin/sh 
counter=0
while read line; do 
  if `echo ${line} | grep "^D" 1>/dev/null 2>&1` 
  then
     counter=`expr $counter + 1` 
  elif 
    `echo ${line} | grep "^T" 1>/dev/null 2>&1` 
  then
    Tval=`echo ${line} | cut -d "," -f 2` 
  fi 
done < csvfile  
if [ $Tval -eq $counter ] 
then
  echo "No of rows matched with T Value" 
else 
  echo "Not matched" 
fi 

Hope this works as you expect

#!/bin/ksh

no_D=`grep "^D" inp.txt | wc -l`
no_T=`grep "^T" inp.txt | wc -l`

if [ $no_D -gt $no_T ]
then
echo "More rows with D"
else
echo "More rows with T"
fi

You can add your logic in if else block.

Hi Nila

in am working in bash shell script.
the cut command is not working.

Tval=\`echo $\{line\} | cut -d "," -f 2\` 

the csv file we using delimeter # so replaced the delimeter in the cut command with # and executed.

it giving error.

It giving what error?

hey watsup guys

i am new in the shell script world. so i need help fom you guys, i have written these two codes and they both give the same errors( expr : syntax error).

Code 1 :

#! /bin/sh
# count1 appends an increment to a file 200 times
# note that a file called numbers must be initialised to contain 0

count=0
while [ $count -lt 200 ] # loop 200 times
do
count=`expr $count + 1` # increment counter
n=`tail -1 numbers` # get last line from numbers file
expr $n + 1 >> numbers # add 1 to it and append it back
done

Code 2:

#! /bin/sh
# count2 also increments and appends a value to the numbers file
# but only when it can successfully create a new hard link to the numbers file

count=0
while [ $count -lt 200 ] # still loop 200 times
do
count=`expr $count + 1` # increment the loop counter
if ln numbers numbers.lock # try to enter the critical region
then # in this case, ln is similar to TSL
n=`tail -1 numbers` # get the last number to increment
expr $n + 1 >> numbers # increment it and append it to the file
rm numbers.lock # exit the critical region
fi # Note that if the ln was unsuccessful, we don't
# do busy waiting, but just continue looping
done

These syntax errors are killing me. i would apreciate if someone could help as soon as possible

Much love

Hi

the error when i execute the script.

: integer expression expected[: 111

thanks.

surubi,
I did try executing both shell scritpts on Solaris 10 server. I did not get any error. Both worked fine.
-Nithin.

Hai barani75,

Check your cut command output in shell. Now also I checked my script that is working fine for your csv file content. I think your environment have some problems. Please check it...

#!/bin/sh
counter=0;
while read line; do
        if `echo ${line} | grep "^D" 1>/dev/null 2>&1`
        then
                counter=`expr $counter + 1`
        elif `echo ${line} | grep "^T" 1>/dev/null 2>&1`
        then
                Tval=`echo ${line} | cut -d "," -f 2`
        fi
done < csvfile

if [ $Tval -eq $counter ]
then
        echo "No of rows matched with T Value"
else
        echo "Not matched"

fi

 					 

Hi surubi,

I just copied your two scripts and I ran it in my bash shell of linux environment.It worked correctly and added the two hundred extra lines into the file.I ran your script by giving the input file numbers with integers only.

For ex,the content of the numbers file as follows.

1
2

After I ran the script,it appended another two hundred lines into the file.

I think your input file may contain string.If it contains string,your scripts won't work.If your input file contains string,then add the following line after the count=0 line in your both scripts.

declare -i n

The above statement converts the character into integer which is required by the expr command.

I have one more doubt in your second script.Why you have created hard link for the file numbers?