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 |
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 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. :) |
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! |
Quote:
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: |
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 Code:
for i in "$@"; do Then when you called your script, you would pass it the filenames you want to check Code:
./my_first_script file1 file2 file3 |
while we are piling on, the following doesnt provide the expected result:
Code:
[schneidz@hyper ~]$ cat fantabulous.txt |
Quote:
|
Quote:
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. |