count and compare no of records in bash shell script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count and compare no of records in bash shell script.
# 1  
Old 02-24-2010
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.
# 2  
Old 02-24-2010
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
https://www.unix.com/shell-programmin...rent-rows.html

Are you trying something similar ?
# 3  
Old 02-24-2010
Code:
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
# 4  
Old 02-24-2010
shell script

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.

Code:
#!/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


Last edited by Franklin52; 02-24-2010 at 08:44 AM.. Reason: Please indent your code and use code tags!
# 5  
Old 02-24-2010
Hope this works as you expect
Code:
#!/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.
# 6  
Old 02-24-2010
cut command for bash shell

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.
# 7  
Old 02-24-2010
It giving what error?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX Script required for count the records in table

Hi Friends, I looking for the script for the count of the records in table. and then it's containg the zero records then should get abort. and should notify us through mail. Can you please help me out in this area i am lacking. (5 Replies)
Discussion started by: victory
5 Replies

2. Shell Programming and Scripting

Script to compare count of two csv files

Hi Guys, I need to write a script to compare the count of two csv files each having 5 columns. Everyday a csv file is recived. Now we need to compare the count of todays csv file with yesterday's csv file and if the total count of records is same in todays csv file and yesterday csv file out... (3 Replies)
Discussion started by: Vivekit82
3 Replies

3. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

4. Shell Programming and Scripting

Nawk script to compare records of a file based on a particular column.

Hi Gurus, I am struggling with nawk command where i am processing a file based on columns. Here is the sample data file. UM113570248|24-AUG-11|4|man1|RR211 Alert: Master Process failure |24-AUG-11 UM113570624|24-AUG-11|4|man1| Alert: Pattern 'E_DCLeDAOException' found |24-AUG-11... (7 Replies)
Discussion started by: usha rao
7 Replies

5. Shell Programming and Scripting

Script to count particular type of records

Hi, I have a huge file containing thousands of records which are of following pattern: TYPE1 { originNodeType : "IVR" originHostName : "AAIVR" originTransactionID : "01310559" originTimeStamp : "20110620192440+0530" hostName : "hhhh" voucher : '0'D rProfileID : "ZZZZ" Before {... (5 Replies)
Discussion started by: madhukar1anand
5 Replies

6. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

7. Shell Programming and Scripting

Shell Script to compare files, check current date and count

Hello - I have written the following basic shell script to count files, compare files and look for a particular strings in a file. Problem 1: How do I define more than 1 file location? #!/bin/bash #this is a test script FILES=$(ls /home/student/bin/dir1, home/student/bin/dir2)... (0 Replies)
Discussion started by: DallasT
0 Replies

8. UNIX for Dummies Questions & Answers

Bash script to extract spf records

Hello I am trying to generate a script to run on worldwide firewalls. I need the spf block for large sites like google, etc so I can essentially whitelist google sites for users. (Google here is just an example...) Right now I am just testing Bash oneliners to see how I can isolate the... (1 Reply)
Discussion started by: mbubb
1 Replies

9. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question