reading and calculating no. of occurrence in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading and calculating no. of occurrence in awk
# 8  
Old 11-14-2010
Thank you but it didn't work.....

I run it as an awk file on the file that contains the only 2 fields ans I direct the results in new file.
awk -f (your code) (two fieldsFile) > newfile

I got an empty file.......!!!

Ithink there is a miss understanding. I need to print the $2 that relate to 1 in a seperate file and also to print $2 that relates 2 in another seperate file.

File1:
$2 | $8
---------
1.2 | 1
1.3 | 1
.
.
all the values should be with the 1

File2:
$2 | $8
---------
2.2 | 2
1.9 | 2
.
.

---------- Post updated at 05:31 PM ---------- Previous update was at 05:29 PM ----------

I don't want to remove ones or two's---- i want to filter $2 based on them>>>
# 9  
Old 11-14-2010
That's what the code I supplied you does, it dosn't write anything to standard out so no need to redirect this to a file.

Instead, it writes to two files named "ones" and "two" (do a ls in the directory you run the script and they should be there). The remove in the BEGIN block is to just clean up these files if they are still around from a previous run of the script.
# 10  
Old 11-14-2010
I'm sorry I searched my directory and found nothing.....!!!!

what do you suggest ?????

The files sould be in which extension???
# 11  
Old 11-14-2010
Don't know what to tell you, here is a transcript from my test:

Code:
$ cat infile
a 1.351 c d e f g 1 h i
a 1.251 c d e f g 1 h i
a 1.751 c d e f g 1 h i
a 1.3   c d e f g 2 h i
a 1.8   c d e f g 2 h i
a 1.9   c d e f g 1 h i
 
$ awk 'BEGIN { system("rm -f ones"); system("rm -f two"); }
($8 == 1) { print $2, $8 >> "ones" }
($8 == 2) { print $2, $8 >> "two" }' infile
 
$ cat ones
1.351 1
1.251 1
1.751 1
1.9 1
 
$ cat two
1.3 2
1.8 2

Could it be that you don't have write privledge to the current directory (try replacing "ones" with "/tmp/ones" in the script).
# 12  
Old 11-14-2010
Thank you maaaan
It works

I was doing the script on the file that I extracted which becomes two field and the script you provided works on the original file which has many fields. Thus, the script was searching for $8 which wasn't there!!!!!


Again thank you and I really Apreciate HOW FAST YOU REPLIED
# 13  
Old 11-16-2010
Hi Chubler_XL,
How are you? I hope you are fine......

I came today with small simple problem.....
just to remind you that in the previous posts you helped me out to first separate the results into two files ( ones, two). Also helped me to count no. of occurrence for each file..

------------------ Now-----------
What if I want to add both scripts to be in one file to avoid running two scripts each time. running one script is much easier and time consuming.
so the logic should be the following.

run the following on a file provided in the shell :
Code:
$ awk 'BEGIN { system("rm -f ones"); system("rm -f two"); }
($8 == 1) { print $2, $8 >> "ones" }
($8 == 2) { print $2, $8 >> "two" }' infile

then run the count script that counts no. of occurence on each file ( "ones", "two")
and the results could be two new file ---> "ones_A", "two_B".
Note: count script should only count no.of occurrence on $1 for each file ("ones", "two").
Code:
BEGIN{ 
    nextTime=incrMent=0.1;
}
{    
    time= $0;
    while (time>nextTime){
        printf "%3.1f - %3.1f  count %d\n", nextTime - incrMent, nextTime, count;
        nextTime+=incrMent;
        count=0;
    }
    no_occurrence++;
    count++;
}END{
    printf "%3.1f - %3.1f  count %d\n", nextTime - incrMent, nextTime, count;
}

Waiting for your reply......
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-16-2010 at 12:00 PM.. Reason: code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculating correlations across columns in awk

Hello, I came across a previous thread "awk-calculating-simple-correlation-rows" which calculated correlations across rows in awk. Code: awk '{ a = 0; for (i = 2; i <= NF; ++i) a += $i; a /= NF-1 b = 0; for (i = 2; i <= NF; ++i) b += ($i - a) ^ 2; b = sqrt(b) if... (7 Replies)
Discussion started by: Ross
7 Replies

2. UNIX for Dummies Questions & Answers

Calculating cumulative frequency using awk

Hi, I wanted to calculate cumulative frequency distribution of my data that involves several arithmetic calls. I did things in excel but its taking me forever. this is what I want to do: var1.txt contains n observations which I have to compute for frequency which is given by 1/n and subsequently... (7 Replies)
Discussion started by: ida1215
7 Replies

3. Shell Programming and Scripting

Calculating Running Variance Using Awk

Hi all, I am attempting to calculate a running variance for a file containing a column of numbers. I am using the formula variance=sum((x-mean(x))^2)/(n-1), where x is the value on the current row, and mean(x) is the average of all of the values up until that row. n represents the total number... (1 Reply)
Discussion started by: Jahn
1 Replies

4. Shell Programming and Scripting

Calculating average with awk

I need to find the average from a file like: data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1... (4 Replies)
Discussion started by: Slagle
4 Replies

5. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. Shell Programming and Scripting

AWK way of calculating growth

Hi All, IS there any 'awk' way to manipulate following data? Fruit Date Count Apple 20/08/2011 5 Apple 27/08/2011 7 Apple 05/09/2011 11 Apple 12/09/2011 3 Apple 19/09/2011 25 . . . . Orange 20/08/2011 9 Orange 27/08/2011 20 Orange 27/08/2011 7 Orange 05/09/2011 15 Orange... (3 Replies)
Discussion started by: aniketdixit
3 Replies

7. Shell Programming and Scripting

Calculating an integer with awk

I would like to extract a number from $0 and calculate if it can be devided by 25. Though the number can also be less then 25 or bigger than 100. How do i extract the number and how can the integer be calculated? String: "all_results">39</span>I am looking for the number between "all_results"> ... (5 Replies)
Discussion started by: sdf
5 Replies

8. Shell Programming and Scripting

Awk program for calculating dates.

Hi All, I have a txt file which has hundreds of lines and 41 fields. I have a requirement to pick up field 14 from the text file which is a date fiels in the following format. Field 14 : Data Type : NUMERIC DATE (YYYYMMDD) Field Length : 8 Example of Data :20090415 Field 42 : Data Type... (2 Replies)
Discussion started by: nua7
2 Replies

9. Shell Programming and Scripting

Calculating totals in AWK

Hello, With the following small script I list the size of documents belonging to a certain user by each time selecting the bytes-field of that file ($7). Now it fills the array with every file it finds so in the end the output of some users contains up to 200.000 numbers. So how can I calculate... (7 Replies)
Discussion started by: Hille
7 Replies

10. UNIX for Dummies Questions & Answers

awk + last occurrence

Hi, I'm attempting to search, using awk, a pattern range in a file. Something like: >awk '/first bit of text.../,/...last bit of text/' file Is it possible to print only the last (or first) occurrence of the pattern range this way? Thanks for any suggestions. Al (2 Replies)
Discussion started by: agibbs
2 Replies
Login or Register to Ask a Question