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
# 8  
Old 09-11-2014
It could look like this:

Code:
awk '
  /~YES~/ {Y++}
  /~NO~/{N++}
  END{
    print "Total Records : " NR
    print "YES : " Y
    print "NO : " N
    print "No Response : " NR - Y - N
  }' $(cat filename.txt)

This User Gave Thanks to Chubler_XL For This Post:
# 9  
Old 09-12-2014
Lightbulb

Thanks Buddy.

can I store this output of awk to other file?

---------- Post updated 09-13-14 at 12:50 AM ---------- Previous update was 09-12-14 at 08:06 AM ----------

I have achieved the required output and saved in file as
Code:
Total Records : 263
YES : 143
NO : 8
No Response : 102

How to do mathematical operations using awk?

For above output i need one more additional field Junk which needs to be calculated from above results as
Junk : Total Records -(YES+NO+No Response)
for above case :
Junk :10 with values 263 -(143+8+102)

And, finally i want to print this output in next line in the same file which have above result
Code:
Total Records : 263
YES : 143
NO : 8
No Response : 102
Junk : 10

# 10  
Old 09-12-2014
Please show us your code. If you had used what Chubler_XL suggested in message #8 in this thread, "Junk" should always be zero. The math from his suggestion when expanded to what you're asking for would be:
Code:
Junk = Total Records - Yes - No - No Response

which expands to:
Code:
Junk = NR - Y - N - (NR - Y - N)

which is zero.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 09-13-2014
Hi Don,

I don't have exact code here with me but i can tell you the process

1. I did merge of all the files using cat
2. then found total count using awk on merged file and put all results into new file
awk -F'~' '($1=="99") { print $2 }' file >>new file


3.found Yes, No , No response using cat |grep |wc -l >>new file
eg .YES: cat file|grep '~YES~' |wc -l

Last edited by Don Cragun; 09-13-2014 at 03:30 PM.. Reason: Add missing ICODE tags.
# 12  
Old 09-13-2014
You have shown us code that adds data to an existing file (with no indication of what was in the file to begin with). You have shown us code that would generate syntax errors.

Please wait until you can show us your EXACT code and post it so we can help you debug it. And post sample data that shows the problems you are facing. (All using CODE tags.)
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 09-15-2014
Hi Don,

Below is the code :

Code:
 
while read LINE
do
cat ${LINE}>>count.xls
done < file.txt
print "Total Records : $(awk -F'~' '($1=="99") { sum+=$2 } END { print sum+0 }' count.xls)">>final_count.xls
print "\n">>final_count.xls
print "YES : $(cat count.xls|grep -i '~Y or YES~' | wc -l)">>final_count.xls
print "NO : $(cat count.xls|grep -i '~N or NO~' |wc -l)">>final_count.xls
print "No Response : $(cat count.xls|grep -i '~No Response~' |wc -l)">>final_count.xls
if [ $? -ne 0 ]
then
echo failed
exit 12
fi


file.txt :
contains list of files.
# 14  
Old 09-15-2014
this line doesn't work:
Code:
print "YES : $(cat count.xls|grep -i '~Y or YES~' | wc -l)

try replace by:
Code:
printf "YES:%s\n" "$(cat count.xls | grep "YES~\|~Y " | wc -l)"

Your line under doesn't work so
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