1.total count : 23 #addition of trailer values for each file '99' suggests it's a trailer
2. YES : 10 # total occurrences of '~YES~'
3. NO : 5 # total ocurrences of '~NO~'
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)
---------- 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
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
Total Records : 263
YES : 143
NO : 8
No Response : 102
Junk : 10
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:
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.)
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?
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:
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:
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.
This code assumes that no more than one of the strings you're looking for will occur on any single line.
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.
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.
I redirected your script's diagnostic message to stderr instead of stdout.
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 >> .
If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .