Awk regular expression - I need exactly 1 occurrence of it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk regular expression - I need exactly 1 occurrence of it
# 8  
Old 01-11-2009
Here is a sample of the desired output

"
0.020064
0.522624
1.00656
8.058944
"

based on this input:

"
- 0.020064 0 1 tcp 40 ------- 1 0.0 6.0 0 0
r 0.020064 1 2 tcp 40 ------- 1 0.0 6.0 0 0
r 0.020067 1 2 tcp 40 ------- 1 0.0 6.0 0 0
...
+ 0.522624 5 6 tcp 40 ------- 2 7.0 6.1 0 1
r 0.522624 1 5 tcp 40 ------- 2 6.1 7.0 0 12
r 0.522625 1 5 tcp 40 ------- 2 6.1 7.0 0 12
...
+ 0.998912 5 6 tcp 1040 ------- 3 8.0 6.2 1 21
r 1.00656 1 2 tcp 40 ------- 4 6.3 9.0 1 26
r 1.00657 1 9 tcp 40 ------- 4 6.3 9.0 1 26
...
r 8.058944 1 5 tcp 1040 ------- 2 7.0 6.1 52 883
r 8.058944 5 6 tcp 1040 ------- 2 7.0 6.1 52 883
r 8.062336 1 6 tcp 1040 ------- 5 10.0 6.4 200 837
"

This is actually the output of an ns-2 simulation, the $2 represents the time. So what I basically want is only the time values that have the form *.0* or *.5* and they have $3==1. Moreover, if there are more than one values with this quality, say 8.058944 and then 8.063296, I only want the first 8.0* value, in this case 8.058944. And this goes also for all the other values (0.0*, 0.5*, 1.0*, 1.5*, ..., 8.0*, 8.5*)

Thanks
# 9  
Old 01-11-2009
If I don't misunderstand the question:

Code:
awk '{f=substr($2,1,3)} f!=s && $3==1{s=f;print $2}' file

Regards
# 10  
Old 01-11-2009
it worked, thank you very much Franklin52 and everybody who answered
# 11  
Old 01-30-2009
Ah reread everything... Saw franklin's solution.
I thought that the 2nd column wasn't necessarily sorted....
so i came up with:

Code:
nawk '{
  if ( $2 ~ /\.[05]/ && $3 == 1 ){
    idx = sprintf( "%0.01f", $2 );
    if ( ! hash[idx] ){
      hash[idx] = $2;
      }
    }
  }
  END{
  for ( idx in hash ){
    print hash[idx];
    }
  }'

The previous solution wouldn't handle 13.0 and 13.5 correctly.... btw....

Last edited by quirkasaurus; 01-30-2009 at 03:14 PM..
# 12  
Old 01-31-2009
thank you, gonna revise the issue,though my problem was mostly solved with the previous suggestion.

regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk regular expression search

Hi All, I would like to search a regular expression by passing as an i/p variableto AWK. For Example :: 162.111.101.209.9516 162.111.101.209.41891 162.111.101.209.9516 162.111.101.209.9517 162.111.101.209.41918 162.111.101.209.9517 162.111.101.209.41937 162.111.101.209.41951... (7 Replies)
Discussion started by: Girish19
7 Replies

2. Shell Programming and Scripting

Problem with Regular expression in awk

Hi, I have a file with two fields in it as shown below 14,30 28,30 16,30 22,30 21,30 3,30 Fields are separated by comma ",". I've been trying to validate the file based on the condition "each field must be a numeric value" I am using HP-UX OS. I have tried the following awk... (4 Replies)
Discussion started by: meetsriharsha
4 Replies

3. Shell Programming and Scripting

awk regular expression

Hello, I have big files which I wanna filter them based on first column. first column should be one of these strings: chr2L || chr2R || chr3L || chr3R || chr4 || chrX and something like chr2Lh or chrY or chrM3L is not accepted. I used the following command: awk '{ if ($1=="chr2L" ||... (5 Replies)
Discussion started by: @man
5 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Regular expression in AWK

Hello world, I was wondering if there is a nicer way to write the following code (in AWK): awk ' FNR==NR&&$1~/^m$/{tok1=1} FNR==NR&&$1~/^m10$/{tok1=1} ' my_file In fact, it looks for m2, m4, m6, m8 and m10 and then return a positive flag. The problem is how to define 10 thanks... (3 Replies)
Discussion started by: jolecanard
3 Replies

6. Shell Programming and Scripting

Awk's variable in regular expression

Anyone know how I will use awk's variable in a regular expression? This line of code of mine is working, the value PREMS should be a variable: awk '$1 ~ /PREMS/ { if(length(appldata)+2 >= length($1)) print $0; }' appldata=$APPLDATA /tmp/file.tmp The value of APPLDATA variable is PREMS. ... (2 Replies)
Discussion started by: Orbix
2 Replies

7. Shell Programming and Scripting

Regular expression query in AWK

Hi, I have a string like this-->"After Executing service For 10 Request" in this string i need to extract "10". the contents of the string is variable and "10" appears before "For" and after "Request" i.e, in this format "For x Request" I need to extract the value of x. How to do this in AWK?... (10 Replies)
Discussion started by: omprasad
10 Replies

8. UNIX for Dummies Questions & Answers

regular expression and awk

I can print a line with an expression using this: awk '/regex/' I can print the line immediately before an expression using this: awk '/regex/{print x};{x=$0}' How do I print the line immediately before and then the line with the expression? (2 Replies)
Discussion started by: nickg
2 Replies

9. Shell Programming and Scripting

awk and regular expression

Ive got a file with words and also numbers. Bla BLA 10 10 11 29 12 89 13 35 And i need to change "10,29,89,25" and also remove anything that contains actually words... (4 Replies)
Discussion started by: maskot
4 Replies

10. Shell Programming and Scripting

Regular expression query in AWK

I have a varable(var1) in a AWK script that contain data in the following format - I need to extract timestamp,priority and log message.I can extract these by using split function but i don't want to use it, since i want to extract it in one go. I have some difficulties in doing it using... (3 Replies)
Discussion started by: omprasad
3 Replies
Login or Register to Ask a Question