LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Searching & counting occurrences of words in multiple text files (https://www.linuxquestions.org/questions/linux-newbie-8/searching-and-counting-occurrences-of-words-in-multiple-text-files-4175510559/)

fantabulous 07-09-2014 05:04 AM

Searching & counting occurrences of words in multiple text files
 
I am female & new to linux programming. I have been reading off the net & attempting to programme to make
Manual tasks easier :)....I started programming last week. I am really excited about getting my first script to work :D


Task: Searching & counting occurrences of words in multiple text files

To count occurrences of the word "cat" & "dog" in a text file (filetext.txt) & display the count value on the screen.

I wrote a script & it works but only when applied to a single file:

------------------------------------------
#!/bin/bash
# My_first_script

a=$(grep -c "cat" filetext.txt)

echo "cat:$a"

b=$(grep -c "dog" filetext.txt)

echo "dog:$b"
------------------------------------------

Additional info:

filetext.txt is in my home directory. I change the contents of the file so in essence I can run the script on "multiple text files"
I feel this is cumbersome & time consuming. I want to optimize/shorten/improve the script.

What I would like to achieve: I would like to apply my script to multiple files at once & get outputs per file.

Guidance will be appreciated:

How can I apply my script to multiple files filetext1.txt filetext2.txt filetext3.txt
& get one output of :

filetext1
cat :3
dog :2

filetext2
cat :3
dog :3

filetext3
cat :2
dog :2


Thanks
Kind Regards
Fantabulous

Philip Lacroix 07-09-2014 05:25 AM

Welcome to LQ! You can use a "for" loop in order to apply your code recursively to your files. Let's say that you want to work on all files in the current directory:

Code:

for i in * ; do
  # Your code applied to "$i"
done

These will be useful sources for Bash scripting:

Bash Guide for Beginners, by Machtelt Garrels
Advanced Bash-Scripting Guide, by Mendel Cooper

Oh, and there are several Scripting Gurus right here on LQ (I'm not one of them), so stay tuned. :)

fantabulous 07-09-2014 08:25 AM

Wow. Thanks so much! That works. Its brilliant :D

Sorry for sounding so excited but lol Linux programming seems to be extremely powerful...

If I have any regrets .... its not starting in "linux" earlier.

Thanks again
Much appreciated!

Philip Lacroix 07-09-2014 01:16 PM

Quote:

Originally Posted by fantabulous
Wow. Thanks so much! That works. Its brilliant

You're welcome, but actually it was not that brilliant: these are the basics, but you can still do a lot of things with them, like automate boring tasks and save a lot of time. I don't know which particular distro you are using, but you might be interested in some general notions regarding Linux and UNIX-like operating systems. When I started I found the following guide very helpful and that's why I recommend it quite often (as a complement to "man" pages, software documentation and more specific guides):

Introduction to Linux - A Hands on Guide, by Machtelt Garrels

The whole Linux Documentation Project is a valuable source of information itself, and of course the LQ forums. Enjoy your stay. :hattip:

suicidaleggroll 07-09-2014 01:20 PM

To build on the above suggestion, you may want to modify your script to loop over some input filenames, rather than looping through everything in the cwd (current working directory). For that, you would change
Code:

for i in *; do
to
Code:

for i in "$@"; do
In bash, "$@" means "all command line arguments"

Then when you called your script, you would pass it the filenames you want to check
Code:

./my_first_script file1 file2 file3

schneidz 07-09-2014 02:18 PM

while we are piling on, the following doesnt provide the expected result:
Code:

[schneidz@hyper ~]$ cat fantabulous.txt
cat dog cat
cat
mouse
dog
[schneidz@hyper ~]$ grep -c dog fantabulous.txt
2
[schneidz@hyper ~]$ grep -c cat fantabulous.txt
2


fantabulous 07-09-2014 05:13 PM

Quote:

Originally Posted by schneidz (Post 5201259)
while we are piling on, the following doesnt provide the expected result:
Code:

[schneidz@hyper ~]$ cat fantabulous.txt
cat dog cat
cat
mouse
dog
[schneidz@hyper ~]$ grep -c dog fantabulous.txt
2
[schneidz@hyper ~]$ grep -c cat fantabulous.txt
2


Hi Schneidz. Thanks for your feedback. You are correct in the the text file sample you provided. Apologies...I should have been more clearer. The text file I am using contains 1 word per line so for this case the solution is working.

fantabulous 07-09-2014 05:17 PM

Quote:

Originally Posted by suicidaleggroll (Post 5201242)
To build on the above suggestion, you may want to modify your script to loop over some input filenames, rather than looping through everything in the cwd (current working directory). For that, you would change
Code:

for i in *; do
to
Code:

for i in "$@"; do
In bash, "$@" means "all command line arguments"

Then when you called your script, you would pass it the filenames you want to check
Code:

./my_first_script file1 file2 file3

Awesomeness:) This will be very useful indeed. I will try this method as well. I am certain I will need to input arguments
in the very near future for other tasks I want to simplify.

Thanks so much for the feedback.


All times are GMT -5. The time now is 04:44 PM.