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
# 1  
Old 01-06-2009
Awk regular expression - I need exactly 1 occurrence of it

Hi all,

I am processing a file with awk that looks like this:

"
0.0021 etc
0.0123 etc
0.1234 etc
...
0.5324 etc
0.5434 etc
0.6543 etc
...
1.0344 etc
1.1344 etc
...
1.5345 etc
1.5632 etc
"
I need to print out only the lines that have '0' or '5' after the comma, plus I need only one occurrence of each pattern match. So, based on the structure above, I need this:

"
0.0021 etc
0.5324 etc
1.0344 etc
1.5345 etc
"

I managed to keep only the lines that have '0' or '5' after the comma with this regular expression (somewhere inside an "if" clause of an awk script):

$1~/0\.0/||$1~/0\.5/||$1~/1\.0/||$1~/1\.5/

so i get this:

"
0.0021 etc
0.0123 etc
0.5324 etc
0.5434 etc
1.0344 etc
1.5345 etc
1.5632 etc
"

What I can't accomplish, is to keep only the fisrt occurrence of each ".0" or ".5" pattern.

I found in the gnu manual that to keep exaclty n occurrences of the expression r in awk, you should add {n} right after (r{n}). So, I tried it with {1} after the expression and it didn't compile. I also tried it with backshlashes and put it before or somewhere inside the expression, but no luck.

Do you have any idea what's wrong?

Thanx in advance.
# 2  
Old 01-06-2009
Code:
awk '$1~/[0-9]\.(0|5)/ && !a[substr($1,1,3)]++' file

# 3  
Old 01-06-2009
Quote:
Originally Posted by rubin
Code:
awk '$1~/[0-9]\.(0|5)/ && !a[substr($1,1,3)]++' file

There should not be any need for the ++ or the substr

Code:
awk '/^[0-9]+\.(0|5)/ && ! a[$0]'

# 4  
Old 01-06-2009
Code:
awk '/^.\.[05]/{if(substr($1,1,3)==a){a=substr($1,1,3);next}print;a=substr($1,1,3)}'

# 5  
Old 01-07-2009
Quote:
Originally Posted by rubin
Code:
awk '$1~/[0-9]\.(0|5)/ && !a[substr($1,1,3)]++' file


Can you please tell me what does this part of the code exactly does --
&& !a[substr($1,1,3)]++'
# 6  
Old 01-07-2009
Thanx everybody for replying.

The problem was not solvedSmilie

It seems that the second part of the expression

$1~/[0-9]\.(0|5)/ && ...2nd part...

is always true if the first part is true. Whether the expression is "!a[substr($1,1,3)]++" or "!a[$0]".

The solution frostmourn suggested unfortunately didn't compile and I am afraid I don't understand the structure that well in order to make it compile.

Just for the record, the actuall file looks like this (I posted a simplified view before, thought it didn't matter):

"
+ 0.1 0 1 tcp 40 ------- 1 0.0 6.0 0 0
- 0.1 0 1 tcp 40 ------- 1 0.0 6.0 0 0
+ 0.1 7 2 tcp 40 ------- 2 7.0 6.1 0 1
- 0.1 7 2 tcp 40 ------- 2 7.0 6.1 0 1
+ 0.1 8 3 tcp 40 ------- 3 8.0 6.2 0 2
- 0.1 8 3 tcp 40 ------- 3 8.0 6.2 0 2
...
...
- 12.999072 2 7 ack 40 ------- 2 6.1 7.0 59 1228
r 13.002496 2 7 ack 40 ------- 2 6.1 7.0 59 1227
r 13.015712 3 2 ack 40 ------- 2 6.1 7.0 59 1229
+ 13.015712 2 7 ack 40 ------- 2 6.1 7.0 59 1229
- 13.015712 2 7 ack 40 ------- 2 6.1 7.0 59 1229
r 13.019136 2 7 ack 40 ------- 2 6.1 7.0 59 1228
r 13.035776 2 7 ack 40 ------- 2 6.1 7.0 59 1229
"

and the '$1' in my previous example is the '$2' in the actual problem. So this is the code I use based on your suggestions (I also need $1 to be "r", $3 to be 1 and $5 to be "tcp" but it doesn't change anything):

if($1=="r" && ($2~/\.(0|5)/ && !a[substr($2,1,3)]++) && $3==1 && $5=="tcp")
{
...
}

this didn't work either:
if($1=="r" && ($2~/\.(0|5)/ && !a[$2]) && $3==1 && $5=="tcp")
{
...
}

still doing something wrong?Smilie

//The code I used before and returned all the occurences and not just the first one was:
if($1=="r" && ($2~ /\.0/ ||$2~ /\.5/) && $3==2 && $5=="tcp")
{
...
}

Thanx in advance.


@rujuta_rahalkar: substr(a,b,c) returns a substring of the string a, that begins at place b (starting from 1) and extends to c places. The effects of the negation and the increment are not clear to me either.

Last edited by ioannisp; 01-07-2009 at 07:48 AM..
# 7  
Old 01-10-2009
Please post a sample of your desired output it is a little unclear what you are trying to achieve.
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