Bash – validate file content with bash Regular Expressions

awkbashlinuxregular expressionsed

How to validate the following file content?

That should be include single integer/float number by bash Regular Expression or any other idea with awk/sed.

example:

cat  /var/VERSION/Version_F35_project_usa
2.8

Best Answer

Use grep, if matched means that's valid:

grep -P '^[0-9]+(\.[0-9]+)?$' infile.txt

The above regex can be used in sed or awk or any command.

sed -n -Ee '/^[0-9]+(\.[0-9]+)?$/p'
awk '/^[0-9]+(\.[0-9]+)?$/'

Here is also checking if file match with this regex or not.

awk '/^[0-9]+(\.[0-9]+)?$/{print "matched";exit} {print "not-matched";exit}' file
Related Question