简体   繁体   English

使用脚本 bash 在文件中搜索特定行(记录)

[英]search for a specific row(record) in a file with script bash

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.我有一个名为 annuaire.txt 的文件,其中包含用户使用读取输入的记录,我想搜索特定的行 $var,如 $nomPersonne 我使用if grep -q $recherche $prenomPersonne annuaire.txt但没有工作。

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就像我有一个读取命令一样,用户可以只输入他正在寻找的字母,但只有在文件的一行中,该行是这些变量之一:$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

You don't need prenomPersonne in your grep.您的 grep 中不需要 prenomPersonne。

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.此方法仅访问“annuaire.txt”文件一次。 Your original approach does it 3 times.您原来的方法做了 3 次。 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.双 grep 允许输入可能的反向输入,但仍会为您提供有效结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM