awk decimal point numbers matching

Hi All,

Can some one help me in identifying the significance of character "$" ,Which is playing critical role in matching decimal point numbers as below.

[sys@localhost ~]$ echo "01#.01"|awk '{if ($0 ~ /^[0-9]+(\.[0-9]*)?$/) print}'
[sys@localhost ~]$ echo "01#.01"|awk '{if ($0 ~ /^[0-9]+(\.[0-9]*)?/) print}'
01#.01
[sys@localhost ~]$ 

Regards,
Rmkganesh.

end of record/line

Hi,

Yes it is ,but wanna know how its eliminating special chars and alphabets.

Can u let me know.

Regards,
Ganesh G.

Rmkganesh,
The awk utility uses extended regular expressions (EREs). The ERE ^[0-9]+(\.[0-9]*)? will match any string in which the 1 or more ( + ) decimal digits ( [0-9] ) followed by zero or one occurrences ( \(expression\)? ) of a period ( \. ) followed by zero or more ( * ) decimal digits ( [0-9] ) appearing at the start of the string ( ^ ). In this case, it matches the 01 at the start of the given string ( 01#.01 ). (I.e., two decimal digits at the start of the string followed by zero occurrences of a decimal point followed by any number of decimal digits.)

When you add a dollar sign to the end of the ERE (i.e., ^[0-9]+(\.[0-9]*)?$ ), the match only succeeds if the match specified above appears at the end of the string. In other words, an ERE with a circumflex ( ^ ) at the start and a dollar sign ( $ ) at the end only matches if the entire string is matched by the the ERE between the circumflex and the dollar sign. Since nothing in the ERE [0-9]+(\.[0-9]*)? matches the octothorpe ( # ) in the middle of the string 01#.01 , the ERE cannot match this string.

1 Like