To check for a number inside the file in UNIX

Hi Gurus

I am a newbie to Unix programming and I am having a difficulty in finding out a number which is present in a file and storing it in a variable so that i can use it in my shell script.

The content of the file "count" is:

Count of the files=11

I need to just store the value 11 in a variable after reading the file so that i can use it in my script.The number can vary.It is not fixed

Is there any specific command to extract only the number from the whole string present in the file please?

Is this a homework assignment?

Is there only one line in your input file?

Have you looked at your shell's built-in read utility?

No this is not a homework assignment.I need the file count as an input to check in my 'if' loop.If the count of the file matches a specific number i need to perform some specific set of instructions.
There is only one line in the input file as mentioned above
I use a bourne shell

Please share the command how you are counting file?

3 examples:

cnt=`tr -dc '[0-9]' < count`
echo "$cnt"
cnt=`sed 's/[^0-9]//g' count`
echo "$cnt"
IFS="=" read junk cnt < count
echo "$cnt"
echo "$junk"
1 Like

I don't understand this part, It confuse me.

Field separator is set to = , so now as far as read is concerned, there are only 2 fields, Count of the files and 11 . The first part will be read into the variable junk and the second part which is the number will be read into the variable cnt

HTH

--ahamed

IFS="=" temporarily sets the environment variable IFS (input field separator) to = ,
the read command reads left from the IFS to the variable junk , and right from it to the variable cnt . The "standard input" is set to the file count , i.e. it reads from the file.

Thanks a lot! This is exactly what i neeeded! :slight_smile: