Number of occurance with multiple conditions??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of occurance with multiple conditions??
# 1  
Old 02-17-2011
Question Number of occurance with multiple conditions??

Hi,

I the following sample of out put:
Code:
+ 6.07875 10 0 cbr 210 ------- 2 10.0 2.3 1461 19715
- 6.07875 10 0 cbr 210 ------- 2 10.0 2.3 1461 19715
+ 6.07875 22 0 cbr 210 ------- 2 22.0 2.9 1301 19716
- 6.07875 22 0 cbr 210 ------- 2 22.0 2.9 1301 19716
r 6.07922 0 1 cbr 210 ------- 1 30.0 2.27 58 19217
+ 6.07922 1 2 cbr 210 ------- 1 30.0 2.27 58 19217
d 6.07922 1 2 cbr 210 ------- 1 30.0 2.27 58 19217
- 6.0793 0 1 cbr 210 ------- 1 28.0 2.25 114 19298
r 6.07959 31 0 cbr 210 ------- 1 31.0 2.28 45 19627
.... .....      .....                       .....                ......

I need to calculate the following;
create the results in two new files:
File1 : contains the only results from id:2 ($7) [ ------- 2 10.0] AND time ($2) AND d ($1).
File2 : contains the only results from id: 1 ($7) [ ------- 1 30.0] AND time ($2) AND d ($1).

Then I need to calculate on each file number of occurances of "d" with relate to time and only from 1 2: explained down:

Code:
------------------------------------------------------------
d Time     from  To                 Id
-------------------------------------------------------------
d 6.07922   1     2  cbr 210 ------- 1 30.0 2.27 58 19217
-------------------------------------------------------------

expected output
Code:
------------------------------------------------------------
time            from               to               NumberOfOccurance
------------------------------------------------------------
6.07922        1                  2                      13
------------------------------------------------------------

Smilie
Best Regards,
Moe
# 2  
Old 02-18-2011
Hi,

I would try to help, but can't understand the solution you are looking for.

I can't figure neither the output of both files nor the conditions to select lines of input file.

Regards,
Birei
# 3  
Old 02-18-2011
Code:
$ awk '$8==2{print $7,$8,$9,$2,$1}' infile  > File1
$ awk '$8==1{print $7,$8,$9,$2,$1}' infile  > File2

$ cat File1
------- 2 10.0 6.07875 +
------- 2 10.0 6.07875 -
------- 2 22.0 6.07875 +
------- 2 22.0 6.07875 -

$ cat File2
------- 1 30.0 6.07922 r
------- 1 30.0 6.07922 +
------- 1 30.0 6.07922 d
------- 1 28.0 6.0793 -
------- 1 31.0 6.07959 r

or by one awk:

Code:
awk '{print $7,$8,$9,$2,$1 > "File" $8}' infile

Don't understand your second request.
# 4  
Old 02-18-2011
Thank you for your reply...

You are right, My explanation was confusing ... I'll try to explain it again..

Suppose I have the following sample:

Code:
r 6.07922 0 1 cbr 210 ------- 1 30.0 2.27 58 19217
+ 6.07922 1 2 cbr 210 ------- 1 30.0 2.27 58 19217
d 6.07922 1 2 cbr 210 ------- 1 30.0 2.27 58 19217
- 6.0793 0 1 cbr 210 ------- 1 28.0 2.25 114 19298

I want to extract the lines with :
Code:
($8==1 && $3==1 && $4==2 && $1=="d" ){ print $1 ,$2 } -->this is file1 
($8==2 && $3==1 && $4==2 && $1=="d" ){ print $1 ,$2 } --> this is file2

Then, File one would be something like ;
Code:
 
d 6.07922

Then I need to calculate number of occerrance of "d" for each set of time . Time increment period is 0.1. such 6.1--6.2--6.3,....

So number of occureence for each interval of time..EXPECTED output:

0.1 1
0.2 5
0.3 10
0.4 7
.. ..

The challange for me ..is how to make all of this in one script file....

Best Regards,
Mohd
# 5  
Old 02-18-2011
Code:
awk '($3==1 && $4==2 && $1=="d" ){ print $1 ,$2 > "File" $8 }' infile

Then how you get the right number, such as 1,5, 10, 7?
Code:
0.1 1
0.2 5
0.3 10
0.4 7

# 6  
Old 02-21-2011
Yes, The numbers are number of occurrence of "d" for each period of time for example (0.1-0.2)
so, if we calculate number of occurrence we can say " we have 5 "d" in period 0.2
and 10 "d" in 0.3 and so on>

Code:
0.1 1--> number of occurrence 
0.2 5--> number of occurrence 
0.3 10 ->number of occurrence 
0.4 7 --> number of occurrence

Best Regards,
Moe
# 7  
Old 02-21-2011
Code:
awk ' { v=int($2 * 10)/10+0.1; C[v]++ ; max=v>max?v:max; }
END { for(i=0.1;i<=max;i+=0.1) printf "%01.1f %d\n", i, C[i] } ' file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract multiple occurance of strings between 2 patterns

I need to extract multiple occurance strings between 2 different patterns in given line. For e.g. in below as input ------------------------------------------------------------------------------------- mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)... (8 Replies)
Discussion started by: sameermohite
8 Replies

2. Shell Programming and Scripting

Multiple occurance of file

Hi all, I have file structure as file.log 84t-rw-r--r-- 1 emily04 us_cms 24492717 Oct 5 13:29 vgtree_84_1_K3L.root 85t-rw-r--r-- 1 emily04 us_cms 50410380 Oct 5 16:06 vgtree_85_1_uZv.root 85t-rw-r--r-- 1 emily04 us_cms 50567380 Oct 5 16:06 vgtree_85_1_hjv.root 86t-rw-r--r-- 1 emily04... (4 Replies)
Discussion started by: emily
4 Replies

3. UNIX for Dummies Questions & Answers

If + multiple conditions

Hello Unix-Forums! It has been a long time since my last post, but finally I've got a new question: I know in case you can use multiple patterns by case $var in a|b|c|ab) and so on. But how would I place an OR between if ] then ... if ] then ... I want to execute the "..." if... (3 Replies)
Discussion started by: intelinside
3 Replies

4. UNIX for Dummies Questions & Answers

count the number of occurance and increment

Hi, I am trying to count the number of occurance of a specific value in a column and increment the variable in the second column accordingly. I have very little information about Unix. As an example, 21 1 32 1 32 2 45 1 56 1 56 2 56 3 73 1 82 1 Thanks, Natasha (2 Replies)
Discussion started by: natasha
2 Replies

5. Shell Programming and Scripting

Help regarding multiple conditions

Hi All, I am new to shell scripting. Can any one say what is wrong in this if statement, that uses multiple conditions if then *************** else if ( -z $pcs && "$night_time_calc" > "$night_time" ) then ******************************** ... (4 Replies)
Discussion started by: ssenthilkumar
4 Replies

6. Shell Programming and Scripting

Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages. I have used below command but it is not working grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt ... (5 Replies)
Discussion started by: sushmab82
5 Replies

7. Shell Programming and Scripting

multiple if conditions

Guys, Im trying to have a script that evaluates multiple conditions : test.sh: if then echo "host $1" else if then echo "host $1" else echo $1 not valid exit 1 fi when I do ./test.sh brazil1 I get: (4 Replies)
Discussion started by: bashshadow1979
4 Replies

8. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

9. Shell Programming and Scripting

How to replace specific text line out of multiple occurance

Hi I would like to replace specific line eg ExitAction = NONE to ExitAction = FALSE under only TASK sipsiproc and other ExitAction = NONE will remain as usual in the file(shell script) The file contains: TASK rgcdproc { CommandLine = $SSHOME/bin/rgcd.exe NewConsole... (5 Replies)
Discussion started by: madhusmita
5 Replies

10. UNIX for Dummies Questions & Answers

multiple conditions in if/then

Hello, I am having trouble with the syntax with a conditional statement in a BASH script involving multiple conditions. Any suggestions would be greatly appreciated! if ; then array=("${array}" "$dnNum" ) fi i receive this error: ./testscript: ' (4 Replies)
Discussion started by: grandtheftander
4 Replies
Login or Register to Ask a Question