Skip to content
Advertisement

Looping through the file,searching nan’s

I have file with 12000 lines,some of them are nan’s I have written bash,to search for these nan’s

#!/bin/bash
re='^[0-9]+([.][0-9]+)?$'

cat file0.txt | while read line 
do
if ! [[ $yournumber =~ $re ]] ; then
   echo "error: Not a number" >&2; exit 1
fi
done

But only this appears on the screen

error: Not a number

What’s wrong with my bash?

Advertisement

Answer

you are using exit statement, that means this script will immediately exit after printing FIRST error: Not a number . So what is the problem with the script?

People may do if grep '^[0-9]+([.][0-9]+)?$' file0.txt then instead.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement