Total count in each category for given file list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Total count in each category for given file list
# 15  
Old 09-15-2014
The line
print "YES : $(cat count.xls|grep -i '~Y or YES~' | wc -l)">>final_count.xls

is working correctly for me as i have data delimited by ~ and I am looking for occurances of field Y or YES.
# 16  
Old 09-15-2014
Quote:
Originally Posted by santoshdrkr
The line
print "YES : $(cat count.xls|grep -i '~Y or YES~' | wc -l)">>final_count.xls

is working correctly for me as i have data delimited by ~ and I am looking for occurances of field Y or YES.
No, you are not. This grep command will only match lines that contain the exact string ~Y or YES~. If you want to match any line that contains ~Y, ~y, YES~, YEs~, YeS~, Yes~, yES~, yEs~, yeS~, or yes~ you need something much more like what protocomm suggested (but for EREs with alternation, you need egrep instead of grep).

Please describe in English exactly what you are hoping to match with your grep commands. Note also since you apparently aren't matching complete fields, your search for NO will also match the lines containing No Resonse. I.e., the lines containing ~NO~ and the lines containing ~No Response~ will both be counted as matching ~NO when doing case insensitive matches. Is that really what you want?
This User Gave Thanks to Don Cragun For This Post:
# 17  
Old 09-15-2014
Quote:
Originally Posted by Don Cragun
No, you are not. This grep command will only match lines that contain the exact string ~Y or YES~. If you want to match any line that contains ~Y, ~y, YES~, YEs~, YeS~, Yes~, yES~, yEs~, yeS~, or yes~ you need something much more like what protocomm suggested (but for EREs with alternation, you need egrep instead of grep).

Please describe in English exactly what you are hoping to match with your grep commands. Note also since you apparently aren't matching complete fields, your search for NO will also match the lines containing No Resonse. I.e., the lines containing ~NO~ and the lines containing ~No Response~ will both be counted as matching ~NO when doing case insensitive matches. Is that really what you want?

Hi Don,

I need 4 different counts with their match for exact values as :
YES : ~Y or YES~
No : ~N or NO~
No Response : ~No Response~
Junk : Other than above 3
# 18  
Old 09-16-2014
OK.

So this is a completely different problem than what you described in the 1st post in this thread. All of the lines you showed us in that post:
Code:
01~12345~Y~YES~aaaaa~can
02~23456~N~NO~bbbbb~can
.
.
.
99~23

are now to be counted as Junk and the counts of all of the other three categories (using your sample input) are to be zero.

So, the following totally untested code (since we have no samples to test) might do what you want:
Code:
awk -F'~' '
$1 == 99 { tr += $2 }
index("~Y or YES~") { y++;next }
index("~N or NO~") { n++;next }
index("~No Response~") { nr++;next }
{j++}
END {   printf("Total Records : %d\n\n", tr)
        printf("YES : %d\n\nNo : %d\n\nNo Response : %d\n\nJunk : %d\n", y, n, nr, j)
}
' $(cat file.txt) > final_count.txt
if [ $? -ne 0 ]
then    echo failed >&2
        exit 12
fi

Notes:
  1. The code in red adds the Total Records printout that the script you showed us in post #13 in this thread produced. If you no longer want that to be included in your output (as shown in post #17 in this thread), remove the code shown in red.
  2. This code assumes that no more than one of the strings you're looking for will occur on any single line.
  3. If the files named in file.txt are Excel spreadsheet files (as implied by your name for your temporary file), this awk script (and your series of grep commands) will not work.
  4. I changed the name of your output file from final_count.xls to final_count.txt because your requested output is a text file; not an Excel spreadsheet file.
  5. I redirected your script's diagnostic message to stderr instead of stdout.
  6. I create (or overwrite) your output file instead of appending to the current contents. If you really want the output from multiple runs of your script to append, change the > in the last line of the awk command to >>.
  7. If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Total record count of all the file present in a directory

Hi All , We need one help on the below requirement.We have multiple pipe delimited .txt file(around 100 .txt files) present on one directory.We need the total record count of all the files present in that directory without header.File format as below : ... (8 Replies)
Discussion started by: STCET22
8 Replies

2. Shell Programming and Scripting

Category and count with awk

I want to categorize and count the as below: Input file: A1 G1 C1 F1 A2 G1 C1 F1 A3 G1 C1 F2 A4 G1 C2 F2 A7 G1 C2 F2 A8 G1 C2 F3 A11 G1 C2 F3 A23 G1 C2 F3 B4 G1 C2 F3 AC4 G2 C3 F4 B6 G2 C4 F4 BB5 G2 C4 F4 A25 G2 C5 F4 B13 G2 C5 F5 D12 G2 C5 F5 D2 G2 C5 F5 (3 Replies)
Discussion started by: aydj
3 Replies

3. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

4. Shell Programming and Scripting

Count total duplicates

Hi all, I have found another post threads talking about count duplicate lines, but I am interested in obtain the total number of duplicates. For example: #file.txt a1 a2 a1 a3 a1 a2 a4 a5 #out 3 (lines are duplicates) Thank you! (12 Replies)
Discussion started by: mikloz
12 Replies

5. UNIX for Dummies Questions & Answers

How to find count total number of pattern in a file …?

How to find count total number of pattern in a file … File contains : a.txt ------------- aaa bbb nnn ccc aaa bbb aaa ddd aaa aaa aaa aaa grep -c aaa a.txt Op: 4 ( But my requirement is should count the total no of patterns as 7 ) (4 Replies)
Discussion started by: Jitten
4 Replies

6. UNIX Desktop Questions & Answers

What is the way to get a total count of students and with highest marks from a file?

I have different things that I was trying to do but am kind of struggling with this since I'm a Linux noob. I have a files with student names ,marks,year school the . What is the most efficient way to get a total count of students and student with highest marks Initially I tried to get a count... (1 Reply)
Discussion started by: anil2103
1 Replies

7. UNIX for Dummies Questions & Answers

Grep bunch of gzip files to count based on category

Started using unix commands recently. I have 50 gzip files. I want to grep each of these files for a line count based particular category in column 3. How can I do that? For example Sr.No Date City Description Code Address 1 06/09 NY living here 0909 10st st nyc 2 ... (5 Replies)
Discussion started by: jinxx
5 Replies

8. UNIX for Advanced & Expert Users

Count total file downloaded using FTP

Hi All, I'm developing a FTP script as below: ftp -v -n <IP_ADDRESS> << EOF user avery jSqaqUU2 lcd /directory/folder/ ascii prompt mget * bye EOF I would like to enhance the script to count the total file downloaded. For example, once the script run i want the message "Total <n>... (1 Reply)
Discussion started by: cas553
1 Replies

9. UNIX for Dummies Questions & Answers

Total file size of a subset list

Hello! I'm trying to find out the total file size of a subset list in a directory. For example, I do not need to know the total file size of all the files in a directory, but I need to know what the total size is of say, "ls -l *FEB08*" in a directory. Is there any easy way of doing this? ... (3 Replies)
Discussion started by: tekster757
3 Replies
Login or Register to Ask a Question