1

I have this file called annuaire.txt with this records which is enter by a user using read and I want to search for a specific row $var like $nomPersonne I used if grep -q $recherche $prenomPersonne annuaire.txt but didn't work.

Like I have a read command the user can enter just a letter of what he's looking for but only in one row in the file the row is one of those variables: $nomPersonne $prenomPersonne $numBureau $numTelephone $adresseEmail

function ajoutPersonne(){
 read -p "Saisir le nom : " nomPersonne
 read -p "Saisir le prénom : " prenomPersonne
 read -p "Saisir le numéro de bureau : " numBureau
 read -p "Saisir le numéro de téléphone : " numTelephone
 read -p "Saisir votre adresse email : " adresseEmail
 echo "$nomPersonne,$prenomPersonne,$numBureau,$numTelephone,$adresseEmail" >> annuaire.txt
}

function rechercherFiche(){
read -p "Rechercher : " recherche
if grep -q $recherche $prenomPersonne annuaire.txt
then
  echo "$(grep -c $recherche annuaire.txt) resultats trouvés !"
  grep -r $recherche annuaire.txt
else
    echo "Non trouvée dans l'annuaire."
fi
}

It's in french don't mind the variables name.

AJLI,Zakaria,10,1234567890,bbb@ajli.fr
pers,perss,20,2345446675,ccc@pers.fr
potion,asterix,9,111111111,ddd@potion.fr
marmite,obelix,33,8687678576,yyy@marmite.fr
Cyrus
  • 84,225
  • 14
  • 89
  • 153
User1010
  • 11
  • 1
  • 1
    What's wrong with your current code? "didn't work" doesn't tell us very much. Is there an error message? Is the output wrong? – tjm3772 Dec 14 '22 at 22:31
  • there is not an errro in the code but there is a problem with this line : if grep -q $recherche $prenomPersonne annuaire.txt i can't get a result from a grep command when iwant to search a specific row like $prenomPersonne – User1010 Dec 14 '22 at 22:34
  • `grep -q $recherche $prenomPersonne annuaire.txt` searches for $recherche inside two files - $prenomPersonne and annuaire.txt. Does this do what you want? `grep $prenomPersonne annuaire.txt | grep -q $recherche` – tjm3772 Dec 14 '22 at 22:37
  • oh okay i see i tried it but didn't work didn't even show a n error message like it's on sleep i want it to search for $recherche in the file annuaire.txt but only in the row $prenomPersonne – User1010 Dec 14 '22 at 22:43
  • When the user hits enter on `read -p "Rechercher : " recherche`, you will be missing an argument in `grep -c $recherche annuaire.txt`, and grep waits for stdin (you) for data where it looks for the string `annuaire.txt`. – Walter A Dec 14 '22 at 23:30

1 Answers1

0

You don't need prenomPersonne in your grep.

My suggested edit of the script (2nd iteration based on comments) is as follows:

function rechercherFiche(){
        while [ true ]
    do
        read -p "Rechercher : " recherche
        if [ "" = "$( echo "${recherche}" | sed "s/[[:alpha:]]//g" | sed "s/[\ \t]//g" | sed "s/[.-]//g" )" ]
        then
            #retour=$(grep -q $recherche $prenomPersonne annuaire.txt)
            retour=$( grep $recherche annuaire.txt )
            if [ -n "${retour}" ]
            then
                #echo "$( echo "${retour}" | grep -c "${recherche}" ) resultats trouvés !"
                echo -e "\t$( echo "${retour}" | wc -l | awk '{ print $1 }' ) resultats trouvés !"
                echo -e "\t${retour}"
                rm -f annuaire.txt
                break
            else
                echo -e "\tNon trouvé dans l'annuaire ...\n"
            fi
        else
            echo -e "\tAucun chiffre permis pour la recherche ...\n"
        fi
    done
}

This approach accesses the "annuaire.txt" file only once. Your original approach does it 3 times. Saving the result as a variable on the first attempt reduces overall overhead.

If you want to search for both first and last names, then your statement should be:

read -p "Rechercher (nom,prenom): " recherche
retour=$( grep "${recherche}" annuaire.txt )

Or you could do it with two data points:

read -p "Rechercher le nom: " nom
read -p "     et le prenom: " prenom
retour=$( grep "${nom}" annuaire.txt | grep "${prenom}" )

The double grep allows for possible reversed entry of input, but will still give you the valid result.

Eric Marceau
  • 1,601
  • 1
  • 8
  • 11
  • yes it works thank you ! I just want ot restrict the research more so it can be only alpha by adding [ [:alpha:] ] but i don't know where i can put [ [:alpha:] ] on $retour – User1010 Dec 15 '22 at 09:39
  • if you put [[:alpha:]] directly on $retour, you will not get any results, because you will block any lines that have telephone numbers, which is likely all of them. Did you intend to say $recherche ? See the updated code above for the modified function. – Eric Marceau Dec 16 '22 at 01:47