LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-09-2014, 05:04 AM   #1
fantabulous
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Rep: Reputation: Disabled
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


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
 
Old 07-09-2014, 05:25 AM   #2
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware{,-current}, OpenBSD
Posts: 373

Rep: Reputation: 419Reputation: 419Reputation: 419Reputation: 419Reputation: 419
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.
 
2 members found this post helpful.
Old 07-09-2014, 08:25 AM   #3
fantabulous
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Wow. Thanks so much! That works. Its brilliant

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!
 
Old 07-09-2014, 01:16 PM   #4
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware{,-current}, OpenBSD
Posts: 373

Rep: Reputation: 419Reputation: 419Reputation: 419Reputation: 419Reputation: 419
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.
 
1 members found this post helpful.
Old 07-09-2014, 01:20 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,393

Rep: Reputation: 2014Reputation: 2014Reputation: 2014Reputation: 2014Reputation: 2014Reputation: 2014Reputation: 2014Reputation: 2014Reputation: 2014Reputation: 2014Reputation: 2014
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
 
2 members found this post helpful.
Old 07-09-2014, 02:18 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,060

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
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
 
1 members found this post helpful.
Old 07-09-2014, 05:13 PM   #7
fantabulous
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
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.
 
Old 07-09-2014, 05:17 PM   #8
fantabulous
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Counting words in an ASCII text file. stf92 Slackware 8 06-19-2011 04:06 PM
Searching multiple words? ritika_sharma Linux - Newbie 7 12-17-2010 02:25 AM
Searching multiple pdf files for text mikemrh9 Linux - General 6 06-24-2009 06:47 AM
How to count occurrences of unique words in a file beeblequix Linux - General 3 01-14-2007 03:47 PM
Counting SLOC of multiple files uraja Linux - Newbie 1 09-12-2003 07:12 PM


All times are GMT -5. The time now is 12:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration