Search for "letters" in a file

coone4life

New Member
Joined
Jul 28, 2022
Messages
2
Reaction score
1
Credits
24
Hello guys,

i had a file called temperature.log
inside this file there are only numbers (another script write numbers into that file)
Sometimes the script runs into an error. At this point the script write letters in this log-file.

i need a command (maybe an if statement) to check if there are letters in this log.

what i have is this:

------------------------------------------------------------------------------------------------------
if test "/home/pi/GardenControl/TemperatureControl/temperature.log | grep -E [[:a-zA-Z:]]";
then echo "0" > /home/pi/GardenControl/TemperatureControl/temperature.log
fi
------------------------------------------------------------------------------------------------------
but this command writes a "0" all the time. even if there is a already a number in this log.

All the scripts runs on a rpi3b+ with debian on it.

can anyone help pls ?
 


coone4life wrote:
inside this file there are only numbers (another script write numbers into that file)
Sometimes the script runs into an error. At this point the script write letters in this log-file.

The following script will take a file and determine whether there are letters in it. You may be able to adapt it for your purposes.

First, here are three test files:
Code:
[len@onk ~]$ cat fileWithNos
123
456
789
0
[len@onk ~]$ cat fileWithText
the quick brown fox jumps over the lazy dog
[len@onk ~]$ cat fileWithNosAndText

the quick brown fox jumps over the lazy dog
123

Here is the script which is called "logtest":
Code:
#!/bin/bash
# This script's name is: logtest
# Test to see if there is a file argument to the script,
# if no argument, show usage format.
if [ $# -ne 1 ]
then
echo "usage: logtest file"
exit 1
fi

# Search the file for letters and do so silently,
# assign the return value of the grep command to
# the variable "no". If no = 0, then it's true
# that letters exist in the file, otherwise "no"
# will be 1 which means there are no letters.
grep -q -e [a-zA-Z] $1
no=$(echo $?)

# test to see if "no" is equal to 0. If it is
# equal to 0, then letters have been found in
# the file. Otherwise, no letters were found.
if [ "$no" == 0 ]
then
echo "letters found in file"
else
echo "no letters found in file"
fi


Running logtest first without an argument, then on each of the files
outputs the following results:

Code:
[len@onk ~]$ ./logtest
usage: logtest file
[len@onk ~]$ ./logtest fileWithNos
no letters found in file
[len@onk ~]$ ./logtest fileWithText
letters found in file
[len@onk ~]$ ./logtest fileWithNosAndText
letters found in file

This script is just functional rather than fully featured.
 
coone4life wrote:


The following script will take a file and determine whether there are letters in it. You may be able to adapt it for your purposes.

First, here are three test files:
Code:
[len@onk ~]$ cat fileWithNos
123
456
789
0
[len@onk ~]$ cat fileWithText
the quick brown fox jumps over the lazy dog
[len@onk ~]$ cat fileWithNosAndText

the quick brown fox jumps over the lazy dog
123

Here is the script which is called "logtest":
Code:
#!/bin/bash
# This script's name is: logtest
# Test to see if there is a file argument to the script,
# if no argument, show usage format.
if [ $# -ne 1 ]
then
echo "usage: logtest file"
exit 1
fi

# Search the file for letters and do so silently,
# assign the return value of the grep command to
# the variable "no". If no = 0, then it's true
# that letters exist in the file, otherwise "no"
# will be 1 which means there are no letters.
grep -q -e [a-zA-Z] $1
no=$(echo $?)

# test to see if "no" is equal to 0. If it is
# equal to 0, then letters have been found in
# the file. Otherwise, no letters were found.
if [ "$no" == 0 ]
then
echo "letters found in file"
else
echo "no letters found in file"
fi


Running logtest first without an argument, then on each of the files
outputs the following results:

Code:
[len@onk ~]$ ./logtest
usage: logtest file
[len@onk ~]$ ./logtest fileWithNos
no letters found in file
[len@onk ~]$ ./logtest fileWithText
letters found in file
[len@onk ~]$ ./logtest fileWithNosAndText
letters found in file

This script is just functional rather than fully featured.

Another way would be to use greps -c option, to count the number of matches and then check the number it outputs.
If the number of matches from grep is zero, there are no letters. If it is greater than zero, there are alphabetic characters/letters in the file.

That way, you don't have to run grep and then check the previous commands return status.

Simply run grep in a sub-shell to get the number of alphabetic characters found in the file and compare it with zero.

Which would look something like this in a script:
Bash:
#!/usr/bin/env bash

# Display an error message and quit if there is a problem
die()
{
    echo "Error: $1"
    echo "Usage: logtest /path/to/file"
    exit 1
}

# Ensure we have one parameter which is a path to a valid file
if [ $# -ne 1 ];then 
    die "Incorrect number of parameters!"
elif [ ! -f $1 ]; then
    die "The file \"$1\" does not exist!"
fi

# Check whether the file contains any alphabetic characters
echo -n "$1 contains "
if [ $(grep -c -e '[a-zA-Z]' $1 ) -ne 0 ]; then 
    echo "letters!" 
else 
    echo "no letters!";
fi

This is a tweaked version of your script.
I've expanded the error checking, to ensure that the single parameter is actually a path to an existing file - a very minor, but useful tweak!

And I've added a "die" function, just to avoid having to duplicate the echo "Usage....";exit 1 part in the first if..elif.... Instead, I just pass in what is different each time - which is an error message!

And obviously - the final tweak is to the grep command. We're running grep in a sub-shell, using the -c switch to count the number of matches and capturing the number it outputs. Which will be the number of alphabetic characters in the file.
Compare that number against zero and we know whether there are any letters in the file! If it's zero, there are no letters. If it's not zero - there are letters!
 
Good result, good input, folks :)

Moving this to Command Line, where it is better situated.

Cheers

Chris Turner
wizardfromoz
 

Staff online

Members online


Top