Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression help 1

Status
Not open for further replies.

learningawk

Technical User
Oct 15, 2002
36
US
I am having trouble understanding how regular expressions work using AWK.
I am trying to match certain characters in a substr field.
For example:
field 1=123-2N
if (match ($1,/^[1][0-9][0-9].?[N])) { print}

In my input file I have several different combinations of like data, such as:

field 1=123-2N
field 1=123-2N1
field 1=123-2N11
field 1=123-2Nb
field 1=223-2N
I would only like the match to occur when it finds a match on the input of
123-2N

Thanks for helping.
I need a quick problem to be solved.
 
BEGIN {
FS=OFS="="
}

$2 ~ /^1[0-9][0-9]*-[0-9]N/
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
It looks like you are confused about how awk
works to me.

Any of these approaches will work:
Code:
fld1=&quot;123-2N&quot; ; if ($1 ~ fld1) {print}
if ($1 ~ /[0-9]+-[0-9][A-Z]/)  {print $1}
if ( (match($1,//[0-9]+-[0-9][A-Z]/)) {
    print substr($1,RSTART,RLENGTH)
}

But they all do different things.
 
Thanks for the response.

Can you also hint on how I would search for the field=
123-2N11 and not the others or all of the records that would contain an start with 1 and have a &quot;N&quot; somewhere following.

Thanks
 
Try this:
for (i=1 ; i <= NF ; i++) {
if ($i == &quot;123-2N11&quot; || $i ~ /1.*N/) {
print $i
}
}
 
Thanks,
I really need a one liner to do the match statement, since I have a script that would include several hundred of these similar match and print statements.

Just to clariy:
I have a file that has similar but unique character strings such as:
123-2N
123-2N1
123-2N11
123-2Nb
223-2N

on different records, and I would like to specifically set some action statements based on the pattern match and then some global actions for like fields.

Depending on the circumstance, I could set the action based on /^1[0-9][0-9] with a N anywhere in the string or I may want to match on specifically /^1[0-9][0-9]b$

Thanks for helping me understand how regular expressions work.
 
I'm having trouble following what you want to do and why
you want to use match() so badly and why you have to have
&quot;one-liners&quot;, etc...
This does what you first described.
if ($0 ~ /^1[0-9]+-.*N/) {print}

If you have some conditional searches you need to do
after a certain line is read I think you are doing things
the hard way, but you could functionally wrap match() if thats what you are getting at.
Code:
function searchPat(line,pat) {
    if ( (match(line,pat)) ) {
         return substr(line,RSTART,RLENGTH)
    }
return &quot;NULL&quot;
}
 
Yea,

The statment :

if ($0 ~ /^1[0-9]+-.*N/) {print}

is performing what I need it to do.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top