0

echo 8d07\'54.520\"W | awk '{ if ($1 ~ /[-+]?[0-9]*[.]?[0-9]+/) print $1; else print "erro" }'

I'm trying to check if it's a number, but it's no working... I use this same regex in a html input text, and it works.

In this case I was expecting "erro". It's not working.

My final goal is to apply 3 different pattern match to 3 fields $1 $2 $3...

1 Answer 1

1

Not 100% sure of the requirement but you probably need to put anchors.

$ echo 8d07\'54.520\"W | awk '{ if ($1 ~ /^[-+]?[0-9]+[.]?[0-9]+/) print $1; else print "erro" }'
erro
Sign up to request clarification or add additional context in comments.

6 Comments

It doesn't validate in the same way... I don't understand.
Without anchors ^ your regex matches any part of your $1 and prints it. If you need to match, you have to tell the regex to start looking from the start of $1 and not anywhere in between $1
OK! I'm trying to understand. In html input this is done by default... This is the correct pattern [-+]?[0-9]*[.]?[0-9]+ because with your if you put one digit it gives erro
I'm reading this more carefully en.wikipedia.org/wiki/Regular_expression This a more complete pattern for number.
The $ at the end is very important the patter like this worked! ^[-+]?[0-9]*[.]?[0-9]+$ Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.