awk decimal point numbers matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk decimal point numbers matching
# 1  
Old 09-16-2013
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.

Code:
[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.

Last edited by Scott; 09-17-2013 at 01:13 AM.. Reason: Code tags
# 2  
Old 09-16-2013
end of record/line
# 3  
Old 09-16-2013
Quote:
Originally Posted by vgersh99
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.
# 4  
Old 09-17-2013
Quote:
Originally Posted by rmkganesh
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.

Code:
[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.
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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Moving decimal point in a series of numbers

Hi, i need to move the decimal point from a file listing some numbers like this : 49899.50 49914.55 49894.48 49939.65 49879.44 49919.57 49934.62 49944.67 49954.72 (1 Reply)
Discussion started by: Board27
1 Replies

3. Shell Programming and Scripting

Check for decimal point and add it at the end if its not there using awk/perl

I have test.dat file with values given below: 20150202,abc,,,,3625.300000,,,,,-5,,,,,,,,,,,,,,,,,,,,,, 20150202,def,,,,32.585,,,,,0,,,,,,,,,,,,,,,,,,,,,, 20150202,xyz,,,,12,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, My expected output is shown below: ... (1 Reply)
Discussion started by: nvk_vinoth
1 Replies

4. Shell Programming and Scripting

using awk to strip out decimal point and trailing integers

Hey guys, I've got an awk string that does a simple calculation which works well. However, I'd really like to be able to strip the decimal point and the trailing ints from it - any ideas on how I'd change the awk string to be able to do that ? I've changed it countless times and each time it... (14 Replies)
Discussion started by: jimbob01
14 Replies

5. Shell Programming and Scripting

Matching Numbers in Bash/AWK

Hi, I need to match up some numbers in one file to the closest numbers in other file and produce an output file. File one (f1.txt) is laid out like this PCode Lon Lat AB10 1AA 57.148235 -2.096648 BB2 3JD 53.728563 -2.47852 LU4 9ET... (4 Replies)
Discussion started by: ian_gooch
4 Replies

6. Shell Programming and Scripting

Insert decimal point for numbers

Hi In Unix, I have a file with some numbers like : 45600 12345 I want to insert a decimal point for these numbers based on user input. If the input is 2, the numbers should be changed to 456.00 123.45 If the input is 3, the numbers should be changed to 45.600 12.345 Can... (2 Replies)
Discussion started by: yoursdivu
2 Replies

7. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

8. Shell Programming and Scripting

Insert a decimal point

Hi all. Using /bin/sh on an HPUX system. I want to place a decimal in the field 2 charactors from the right (yes, converting to currency). The field lengths are variable. Here's what I'm doing: exec < filename while read FIELD1 FIELD2 do FIELD1="echo $FIELD1 | sed 'syntax that will... (4 Replies)
Discussion started by: lyoncc
4 Replies

9. Shell Programming and Scripting

how to get rid of decimal point?

Hi, I have input with decimal point ( 9.99 ) for hours variable hrs. I need to change it to seconds. Here is my code: secs=`/usr/ucb/echo $hrs*3600 |bc` But I don't want to see the decimal point. I can use awk to trim it if there is one. I am just wondering if there is better standard... (2 Replies)
Discussion started by: cin2000
2 Replies

10. Shell Programming and Scripting

problem with floating point numbers in awk

hi all, i have the following problem using awk in a script i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this num = $4 i cant find a way to convert the variable from string to floating point to perform... (7 Replies)
Discussion started by: kanagias
7 Replies
Login or Register to Ask a Question

Featured Tech Videos