Find similar entry in a .txt file acting as a database.

May i know how do i go along finding similar entry in a .txt file, which is used a as a "database" and post and error saying the entry existed when we key in the entry.

---------- Post updated at 05:18 PM ---------- Previous update was at 05:16 PM ----------

i mean post an error saying the entry existed.

Hi, could you make an example of your problem?

Lets say there is this database "worker.txt" with john:25 where john is the name and 25 is the age.

The program will program to ask use to enter our entry.

echo -n "Name: "
read Name
echo -n "Age: "
read Age

from here, i enter John and then 25.

So how do i go about finding "John" and "25" in the worker.txt where John and 25 already existed. as is already existed, the program will prompt "Worker existed" for example. i having trouble going about finding similar entry in my .txt.

You could test the return code from the grep

name="john"
age=25
file=txt
grep  -x ${name}:${age} $file 2>&1 > /dev/null

if [ "$?" -eq "0" ]; then
        echo "User exists"
fi

---------- Post updated at 12:02 PM ---------- Previous update was at 11:37 AM ----------

One subtle thing I noticed in your initial post - you asked to find "similar" entries, but please note that the -x flag for grep that I posted above will find exact matches. This is probably what you mean.

1 Like

Yea, as there might be chances of 2 John but of different age, i must code in a way that they find the John of that particular age. In addition, is this the similar way to use for delete? just tat i have to change the -x to -v?

Hi,

you would want both -x and -v, to ensure you omit the exact match.