awk - match positive whole numbers and floating point numbers only

Shreyas P V

I have an input file in .csv format which contains entries of tax invoices separated by pipe.

for example:

Header--TIN | NAME | INV NO | DATE | NET | TAX | OTHERS | TOTAL
Record1-29001234768 | A S Spares | AB012 | 23/07/2016 | 5600.25 | 200.70 | 10.05 | 5811.00
Record2-29450956221 | HONDA Spare Parts | HOSS0987 |29/09/2016 | 70000 | 2200 | 0 | 72200

The record's NET value, TAX Value, OTHER Charges and TOTAL value column may contain positive whole numbers or positive floating point numbers with 2-4 places after the decimal point.

Now my requirement is to check whether the columns meets the specified constraints by checking with appropriate 'Regular Expression using awk'. I need to match these 4 columns with regular expression such that if I encounter any numeric value other than positive whole number or positive floating point number , I need to print an error message to the user.

I've tried the following , but it doesn't seem to work.

if(!($5 ~ /[0-9]+/) || !($5 ~ /[0-9]+[.][0-9]+/) || ($5<=0))
    { printf("NET VALUE (Violates constraints)" }

Can anyone give the proper working regular expression or any implementation using built-in-function to meet my requirements?

Tom Fenech

Sounds like your validation should be:

$5 ~ /^[0-9]+(\.[0-9]{2,4})?$/

If it matches that, then it's valid (either a positive whole number, or a number followed by . and between 2 and 4 other numbers).

The anchors to the start and end of the field are important!

As rightly pointed out in the comments, if you want to accept numbers with no digits before the decimal point, then you will have to go for a more complex regular expression.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Awk floating point numbers

From Dev

Matching regex for positive and negative numbers with floating point

From Dev

Bash (Yad) using floating point numbers instead of whole numbers

From Dev

Extract positive and negative floating point numbers from a string in php

From Dev

PHP formatting of positive and negative floating point numbers with leading zeros

From Dev

Extracting positive/negative floating-point numbers from a string

From Dev

HTML input for Positive Whole Numbers Only (Type=number)

From Dev

Floating Point Numbers

From Dev

Floating point numbers in bash

From Dev

product of floating point numbers

From Dev

Negative floating point numbers are not sorted correctly using awk or sort

From Dev

How to handle floating point numbers in Mac OS X awk?

From Dev

Regex matching only floating point numbers in a long text

From Dev

Floating point numbers take only two digit after number

From Java

Big numbers and floating point numbers in JavaScript

From Dev

Convert floating point numbers to decimal numbers

From Dev

SSE 2 and above - Why floating point data types store only 2 floating point numbers

From Dev

Floating point numbers precision in python

From Dev

Rounding small floating point numbers

From Dev

Testing floating point numbers equality

From Dev

Floating point numbers, precision, and Parsec

From Dev

Comparing floating point numbers in D

From Dev

JSON standard - floating point numbers

From Dev

Range of floating-point numbers

From Dev

Printing floating point numbers in assembler

From Dev

Compare floating point numbers as integers

From Dev

PyMongo misbehaving with floating point numbers

From Dev

Nixdorf 8812 Floating Point Numbers

From Dev

Storing floating point numbers in a file

Related Related

  1. 1

    Awk floating point numbers

  2. 2

    Matching regex for positive and negative numbers with floating point

  3. 3

    Bash (Yad) using floating point numbers instead of whole numbers

  4. 4

    Extract positive and negative floating point numbers from a string in php

  5. 5

    PHP formatting of positive and negative floating point numbers with leading zeros

  6. 6

    Extracting positive/negative floating-point numbers from a string

  7. 7

    HTML input for Positive Whole Numbers Only (Type=number)

  8. 8

    Floating Point Numbers

  9. 9

    Floating point numbers in bash

  10. 10

    product of floating point numbers

  11. 11

    Negative floating point numbers are not sorted correctly using awk or sort

  12. 12

    How to handle floating point numbers in Mac OS X awk?

  13. 13

    Regex matching only floating point numbers in a long text

  14. 14

    Floating point numbers take only two digit after number

  15. 15

    Big numbers and floating point numbers in JavaScript

  16. 16

    Convert floating point numbers to decimal numbers

  17. 17

    SSE 2 and above - Why floating point data types store only 2 floating point numbers

  18. 18

    Floating point numbers precision in python

  19. 19

    Rounding small floating point numbers

  20. 20

    Testing floating point numbers equality

  21. 21

    Floating point numbers, precision, and Parsec

  22. 22

    Comparing floating point numbers in D

  23. 23

    JSON standard - floating point numbers

  24. 24

    Range of floating-point numbers

  25. 25

    Printing floating point numbers in assembler

  26. 26

    Compare floating point numbers as integers

  27. 27

    PyMongo misbehaving with floating point numbers

  28. 28

    Nixdorf 8812 Floating Point Numbers

  29. 29

    Storing floating point numbers in a file

HotTag

Archive