awk to count condition in field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to count condition in field
# 1  
Old 12-09-2015
awk to count condition in field

I am trying to confirm the counts from another code and tried the below awk, but the syntax is incorrect. Basically, outputting the counts of each condition in $8. Thank you Smilie

Code:
awk '$8==/TYPE=snp/ /TYPE=ins/ /TYPE=del/ {count++} END{print count}' "C:\Users\cmccabe\Desktop\counts\TSVC_004_HIQ.txt" > "C:\Users\cmccabe\Desktop\counts\count.txt"

Desired output
Code:
snp=5000
ins=200
del=100


Last edited by cmccabe; 12-09-2015 at 10:52 AM.. Reason: fixed format
# 2  
Old 12-09-2015
Would that work?
Code:
awk '$8 ~ /TYPE/ {split($8, type, "="); count[type[2]]++} END{for(t in count){print t"="count[t]}}' "C:\Users\cmccabe\Desktop\counts\TSVC_004_HIQ.txt" > "C:\Users\cmccabe\Desktop\counts\count.txt"

# 3  
Old 12-09-2015
in the spirit of cmccabe:

Code:
awk '$8=="snp"{a++};$8=="ins"{b++};$8=="del"{c++}END{print "snp="a "\n" "ins="b "\n" "del="c}'

# 4  
Old 12-09-2015
Quote:
Originally Posted by protocomm
in the spirit of cmccabe:

Code:
awk '$8=="snp"{a++};$8=="ins"{b++};$8=="del"{c++}END{print "snp="a "\n" "ins="b "\n" "del="c}'

$8 appears to contain the string TYPE=snp, etc..., therefore that would not do. Perhaps:

Code:
awk '$8 ~ /snp/ {a++} $8 ~ /ins/ {b++} $8 ~ /del/ {c++}END{print "snp="a "\n" "ins="b "\n" "del="c}'

This User Gave Thanks to Aia For This Post:
# 5  
Old 12-09-2015
Ok, i correct...

Code:
awk '$8=="TYPE=snp"{a++};$8=="TYPE=ins"{b++};$8=="TYPE=del"{c++}END{print "snp="a "\n" "ins="b "\n" "del="c}'

This User Gave Thanks to protocomm For This Post:
# 6  
Old 12-09-2015
Try also
Code:
awk 'sub (/^TYPE=/, "", $8) {CNT[$8]++} END {for (c in CNT) print c "=" CNT[c]}' file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 12-17-2015
Thank you all Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

3. Shell Programming and Scripting

awk to adjust text and count based on value in field

The below awk executes as is and produces the current output. It isvery close but what Ican not seem to do is add the -exon..., the ... portion comes from $1 and the _exon is static and will never change. If there is + sign in $4 then the ... is in acending order or sequential. If there is a - in... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

awk to remove lines where field count is greather than 1 in two fields

I am trying to remove all the lines and spaces where the count in $4 or $5 is greater than 1 (more than 1 letter). The file and the output are tab-delimited. Thank you :). file X 5811530 . G C NLGN4X 17 10544696 . GA G MYH3 9 96439004 . C ... (1 Reply)
Discussion started by: cmccabe
1 Replies

6. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

7. Shell Programming and Scripting

awk to replace a specific field in certain condition

Hi, I have a file like below PRUM,67016800 ,CC ,C1,67016800 , ,Y,Y,2 ,CK,BX,FOX ,00000001,EA,00000001,20141120 00:00:00, ,N,Y,Y,CK ABCDEF... (7 Replies)
Discussion started by: mady135
7 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Awk: count occurrence of each character for every field

Hi, let's say an input looks like: A|C|C|D A|C|I|E A|B|I|C A|T|I|B as the title of the thread explains, I am trying to get something like: 1|A=4 2|C=2|B=1|T=1 3|I=3|C=1 4|D=1|E=1|C=1|B=1 i.e. a count of every character in each field (first column of output) independently, sorted... (4 Replies)
Discussion started by: beca123456
4 Replies

9. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

10. Shell Programming and Scripting

Awk-Group count of field

Hi, Suppose if i am having a file with following records as given below. 5555 6756 5555 4555 4555 6767 how can i get the count of each record using AWK. Eg:5555 count should be 2 4555 count should be 2 6767 count should be 1 ... (5 Replies)
Discussion started by: tinivt
5 Replies
Login or Register to Ask a Question