1

I'm trying to find the occurence of elements list(from a text file) in a directory.

Below is the Bash code I'm using ,but I'm unable to get the output of grep command on to console.

!/bin/bash
FILENAME=$1
count=0
while read LINE
do
  let count++
  echo "$count $LINE"
  grep -r $LINE /home/user/vaishnavi
done < $FILENAME

echo -e "\nTotal $count Lines read"

Output:

1 ASK
2 TELL 
3 ORDER 
4 NUMBER 
5 SIZE 
6 BASKET 
7 FRUIT 
8 VEGGIES

Total 8 Lines read

I'm getting only the list of elements but not their occurences in the specified location.

Is there anything wrong with my code?

Thanks.

SLePort
  • 15,211
  • 3
  • 34
  • 44
Vaishanavi
  • 11
  • 1
  • use double quote `"$LINE"` in `grep` command. – P.... Feb 24 '17 at 14:07
  • Thanks for your reply .But the output is not varying,it is still not showing me the grep results. – Vaishanavi Feb 24 '17 at 14:18
  • 1
    Can you give us an example of input files and of the desired result? – vdavid Feb 24 '17 at 14:49
  • @vdavid here is the input file i.e. List.txt ASK TELL ORDER NUMBER SIZE BASKET FRUIT VEGGIES Output should be like: 1 ASK It's refernces are (should display all "ASK" references in the specified Path) /home/user/shared/datafeed/etc/fields.xml: Binary file /home/user/shared/bin/x86_win32sMTd.lib matches Binary file /home/user/bin/x86_win32sMTd.lib matches Likewise it should display all the references of elements present in List.txt in the same order specified in Txt file. – Vaishanavi Feb 24 '17 at 16:54
  • @Vaishanavi Then your code looks fine. The only thing I see is that you want to reach files such as /home/user/shared/datafeed/etc/fields.xml but your `grep` command searches /home/user/vaishnavi, which is not a parent folder. If this is your issue, try `grep -r "$LINE" /home/user/shared` – vdavid Feb 24 '17 at 17:53

1 Answers1

0

You need to echo the result of the grep, for example:

echo $(grep -r $LINE /home/user/vaishnavi)
cmbarbu
  • 4,354
  • 25
  • 45