I am writing a Script in which one of the functions is to search in a text file DNI or Name that the user is asked for and when they are found, print the entire lines.
The file has the format "DNI | Name"
2:Pepe
21:Julio
1:Marta
The function buscarNombre
works correctly for me:
function buscarNombre()
{
echo Introduce Nombre a buscar:
read nombrex
nombrex=`grep "\$nombre\b" agenda.txt`
echo $nombrex
menu
}
But the searchDNI function, instead of searching and printing a specific DNI, searches for me and prints any DNI that contains the number that I have passed to them.
function buscarDNI()
{
echo Introduce DNIx a buscar:
read DNIx
DNIx=`grep "\$DNIx\b" agenda.txt`
echo $DNIx
menu
}
If, for example, I tell it to look for the DNI:2, it will print the lines 2:Pepe and 21:Julito and I only want it to print 2:Pepe.
How can I improve buscarDNI
so that it only searches for the exact DNI?