awk calculation wrong field output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk calculation wrong field output
# 1  
Old 05-19-2016
awk calculation wrong field output

The awk below is close but I can't seem to fix it to produce the desired output. Thank you Smilie.

current awk with output
Code:
awk '{c1[$3]++; c2[$3]+=($2)}                                                                 
     END{for (e in c1) print e, c1[e], c2[e]}' input
EFCAB5 2 50
USH2A 2 19

desired output ($1 and $3 values from above)
Code:
EFCAB5 50
USH2A 19

# 2  
Old 05-19-2016
What's your input?
# 3  
Old 05-19-2016
I apologize for that, it is just a text file in the below format:

4 fields ($1=location, $2=count, $3=id,$4=length)
Code:
chr1:123-456 2 EFCAB5 25
chr1:124-457 5 EFCAB5 25
chr2:1234-5678 3 USH2A 15
chr2:1235-5679 2 USH2A 4

The fields in bold (id and sum of matching lengths) are the ones in the desired output. Thank you Smilie.

Last edited by cmccabe; 05-19-2016 at 07:19 PM.. Reason: corrected input
# 4  
Old 05-19-2016
Code:
awk '{ A[$3] += $4 } END { for(X in A) print X, A[X] }' inputfile

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-19-2016
Would that do?
Code:
$ awk '{A[$3] += $4} END{for (i in A) print i, A[i]}' cmccabe.fie
EFCAB5 50
USH2A 19

This User Gave Thanks to Aia For This Post:
# 6  
Old 05-19-2016
So the id is read into the A array and a loop captures the matching lengths. I am not sure what X or i is? Sorry scientist trying to learn. Thank you Smilie.
# 7  
Old 05-19-2016
'for(X in A)' is a loop over every array index X in array A. The loop isn't for capturing it, just printing it after they're all read.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to print output based on first field.

Hi Folks, I have one requirement, There is one file, which contains two fields. Based on first field, I need to print an output. Example will be more suitable. Input file like this. abc 5 abc 10 xyz 6 xyz 9 xyz 10 mnp 10 mnp 12 mnp 6 (2 Replies)
Discussion started by: Raza Ali
2 Replies

2. Shell Programming and Scripting

awk to output the percentage of a field compared to length

The awk below using the sample input would output the following: Basically, it averages the text in $5 that matches if $7 < 30 . awk '{if(len==0){last=$5;total=$7;len=1;getline}if($5!=last){printf("%s\t%f\n", last,... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

awk truncating first field output?

Hello, I'm writing an Awk script to take a command line argument (student's name) and output their relevant student#, name, and marks. For some reason, awk arbitrarily removes the first digit from the student number and doesn't show me the proper output. Here is my code: #! /usr/bin/awk -f... (6 Replies)
Discussion started by: trashmouth12
6 Replies

4. UNIX for Dummies Questions & Answers

awk - output field separator

In awk, how do I print all fields with a specified output field separator? I have tried the following, which does not print the output FS: echo a b c d | awk 'BEGIN{OFS = ";"}{print $0}' (3 Replies)
Discussion started by: locoroco
3 Replies

5. Shell Programming and Scripting

awk calculation automatically rounding off the output

I have some calculation in my script which is similar to the below example . I find that sometimes when using large decimal digits, the output gets automatically rounded off and it is affecting the program. I am not able to understand what is happening here.. awk '{ a=6.32498922 a1=6.324... (5 Replies)
Discussion started by: wanderingmind16
5 Replies

6. Shell Programming and Scripting

awk output field delimiter

Dear All, 1.txt (tab in between each value in a line) a b c a b c a c d you can see below, why with ~ i can output with tab, but = cannot? # awk -F'\t' '$2 ~ /b/' 1 a b c a b c # awk -F'\t' '$2 = "b"' 1 a b c a b c a b d ... (1 Reply)
Discussion started by: jimmy_y
1 Replies

7. Shell Programming and Scripting

Using AWK to format output based on key field

I have file which contains gene lines something like this Transcript Name GO POPTR_0016s06290.1 98654 POPTR_2158s00200.1 11324 POPTR_0004s22390.1 12897 POPTR_0001s11490.1 POPTR_0016s13950.1 14532 POPTR_0015s05840.1 13455 POPTR_0013s06470.1 12344... (6 Replies)
Discussion started by: shen
6 Replies

8. Emergency UNIX and Linux Support

getting wrong output with AWK command!!!

i have a file which gets appended with 9 records daily and the file keeps growing from then...i use to store the previous day files count in a variable called oldfilecount and current files count as newfilecount.my requirement is that i need to start processing only the new records from the... (3 Replies)
Discussion started by: ganesh_248
3 Replies

9. Shell Programming and Scripting

Supressing and replacing the output of a field in Awk

Wondering if anybody can help with changing the output of a field. I'm needing to change the output of a field in this file: User Process ID Time Active Licences Type ChangeAdmin (Phys-agsdev/19353 212), start Wed 1/21 6:30 (linger: 1800) u414013 (Phys-agsdev/19353 1491), start Wed 1/21 12:54... (5 Replies)
Discussion started by: Glyn_Mo
5 Replies

10. Shell Programming and Scripting

how to include field in the output filename of awk

Im using awk and I want the output filename to contain the first field of the input file. Ex. 1 dddd wwwww 1 eeeee wwww 1 wwww eerrrr 2 eeee eeeeee I want the output files to be xxx1 and xxx2 Thank you (4 Replies)
Discussion started by: yahyaaa
4 Replies
Login or Register to Ask a Question