Reputation: 141
I have a subdirectory called Liste
and I have texte
files inside, I want to find a text (a telephone number inside):
read $number
while ! grep -l $number * Liste; do
echo"Le fichier n'existe pas"
read $number
done
grep -l $number * Liste
But it doesn't work. Thanks.
I made a mistake here's my code, and it shows me 2 time the ./Liste/"name"
Upvotes: 0
Views: 441
Reputation: 189487
The problem is that the argument to read
is a variable name, not its value. In other words, drop the dollar sign.
read number
while ! grep -iq "$number" Liste/*; do
echo "Le numero n'existe pas"
read number
done
echo " Le numero existe"
Note also how to quote properly, and how to refer to all the files in the directory Liste
. Finally, the -q
option suppresses the normal output from grep
(it would print every match -- every line in every file in the directory if you entered something like .
!) Finally, notice also that the space after echo
is mandatory.
Update: If you want to list the file names with matches without the leading directory name, cd
into the directory first.
cd Liste
read number
while ! grep -il "$number" *; do
echo "Le numéro n'existe pas"
read number
done
echo "Le numéro existe"
(I added the -l
option on the assumption that you only want the file name; but if you don't, just remove it.)
Upvotes: 2
Reputation: 22831
You can use:
grep "$number" ./Liste/* >/dev/null && echo "Le numero existe" || echo "Le fichier n'existe pas"
Or if you want to output the name of the file that matches, use the l
option:
grep -l "$number" ./Liste/*
Based on your comment, use:
grep -l "$number" Liste/* | xargs basename
Upvotes: 0