LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 09-14-2016, 03:19 PM   #1
bigtony
LQ Newbie
 
Registered: Sep 2016
Location: near chicago
Distribution: linix mint & SME server 9.0
Posts: 3

Rep: Reputation: Disabled
search file for a word, if found run script1, if not found run script 2


i need to search a file and if it contains a certain word then execute script 1 and if NOT found execute script 2. What procedure would i use to search for a word in a file? examples of doing this sort of thing would also be helpful.
thank you - big tony
 
Old 09-14-2016, 03:41 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
What have you tried so far?
 
Old 09-14-2016, 03:58 PM   #3
bigtony
LQ Newbie
 
Registered: Sep 2016
Location: near chicago
Distribution: linix mint & SME server 9.0
Posts: 3

Original Poster
Rep: Reputation: Disabled
i have tried the below, which tells me that there are 2 words "PASSED" in the file. This is the answer i want.
i do not know how to run this so that if the output is 2 then run script1 and if the output is NOT 2 to run script2
your guidance would be appreciated. - thanks tony

working expression below:
cat edge_summary.txt | grep -c PASSED
2
 
Old 09-14-2016, 04:37 PM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Given
Code:
bash-4.3$ cat date.txt
Sun Sep  6 13:16:38 AEST 2015
then
Code:
bash-4.3$  grep -q Aug date.txt && echo "$? Found" || echo "$? Absent"
1 Absent
bash-4.3$  grep -q Sep date.txt && echo "$? Found" || echo "$? Absent"
0 Found

Last edited by allend; 09-14-2016 at 04:49 PM.
 
1 members found this post helpful.
Old 09-14-2016, 09:16 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If the number of found entries is important, store this information in a variable and then perform a simple 'if' to test equal or not.
 
Old 09-14-2016, 09:29 PM   #6
bigtony
LQ Newbie
 
Registered: Sep 2016
Location: near chicago
Distribution: linix mint & SME server 9.0
Posts: 3

Original Poster
Rep: Reputation: Disabled
so how do i store the number of found entries in a variable
and how do i use if to test for equal or not equal?
 
Old 09-14-2016, 09:45 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by bigtony View Post
so how do i store the number of found entries in a variable
and how do i use if to test for equal or not equal?
How? By reading, of course.

Variables
BASH Programming - Introduction HOW-TO
rtmistler's Bash Scripting for Dummies and Geniuses

Read / Study / Ask.
 
Old 09-15-2016, 12:23 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Any search on pretty much any search engine would answer all those questions .. at least try and put in some effort.
 
Old 09-15-2016, 08:07 AM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Quote:
the number of found entries
Is that the number of lines,
Code:
(( $(grep -c Aug date.txt) == 2 )) && ...
or, the number of occurrences?
Code:
(( $(grep -o Aug date.txt | wc -l) == 2 )) && ...
 
  


Reply



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
Bash script help -- Script2 calls Script1 and supplies input to script1. ckacosta Linux - General 1 11-02-2010 03:21 PM
Loop or check SQlite3 database continuously, when query found run script for first 3 Techno Guy Linux - Newbie 3 09-27-2009 07:46 PM
Run commands found in plain text file splunk Linux - Software 6 02-02-2008 11:15 AM
Can't seem to run a simple shell script error command not found each time SimonT Linux - Newbie 5 10-29-2006 06:31 AM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 01:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:15 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
Open Source Consulting | Domain Registration