Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. I have 2 files.
  2.  
  3. The first file contains a list of names. The second file is a book. I want to count how many times each name occurs in the book.
  4.  
  5. I found this Stackoverflow answer: https://unix.stackexchange.com/a/2245/110855
  6.  
  7. I want to use grep -c '\<WORD\>', whoever, I believe the variable is lost in my pipe.
  8.  
  9. Example code:
  10.  
  11. # Assume first file is names.txt and second file is book.txt
  12.  
  13. book=$(cat book.txt)
  14. cat names.txt | while read name; do
  15. echo $book | grep -c '\<$name\>'
  16. done
  17.  
  18. The above code simply prints out 0. I would assume it would iterate through each name. If I execute
  19.  
  20. echo $book | grep -c '\<John\>'
  21.  
  22. it works as expected and prints how many times the name John appears in the book. I'm assuming this is an issue with losing the variable in the pipe.
  23.  
  24. Any help? Thanks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement